二、几个类
//文档类
TiXmlDocument*
pDocument = new TiXmlDocument();
//声明类
TiXmlDeclaration*
pDeclaration = new
TiXmlDeclaration("1.0","UTF-8","");
pDocument->LinkEndChild(pDeclaration);
//注释类
TiXmlComment* pComment
= new TiXmlComment();
pComment->SetValue("Person
TinyXML" );
pDocument->LinkEndChild(pComment);
//元素类
TiXmlElement *pElement
= new TiXmlElement("Person");
pElement->SetAttribute("index", 0); //属性
pDocument->LinkEndChild(pElement);
//文本类
TiXmlText *pText = new
TiXmlText("name");
pElement->LinkEndChild(pText);
//保存
pDocument->SaveFile("test.xml");
三、增加
void MainWindow::addData()
{
TiXmlDocument*
pDocument = new TiXmlDocument();
if(pDocument->LoadFile(m_fileName.toStdString().c_str())) //加载xml,test.xml存在时
{
//清空文件内容
pDocument->Clear();
}
//声明类
TiXmlDeclaration*
pDeclaration = new
TiXmlDeclaration("1.0","UTF-8","");
pDocument->LinkEndChild(pDeclaration);
//注释类
TiXmlComment* pComment
= new TiXmlComment();
pComment->SetValue("Person TinyXML" );
pDocument->LinkEndChild(pComment);
//元素类
TiXmlElement *pRootLv1
= new TiXmlElement("Person"); //创建一个根结点
pDocument->LinkEndChild(pRootLv1);
//添加老师
addTeacher(pRootLv1);
//添加学生
addStudent(pRootLv1);
//保存
pDocument->SaveFile();
}
void MainWindow::addTeacher(TiXmlElement *pRoot)
{
TiXmlElement *pRootLv1
= new TiXmlElement("Teachers"); //创建一个节点
pRoot->LinkEndChild(pRootLv1); //链接到节点pRoot下
int index = 0;
addTeacherData(pRootLv1, index, "赵老师", 28);
index++;
addTeacherData(pRootLv1, index, "王老师", 29);
}
void MainWindow::addTeacherData(TiXmlElement *pRoot, int index,
QString name, int age)
{
TiXmlElement *pRootLv1
= new TiXmlElement("Teacher"); //创建一个节点
pRoot->LinkEndChild(pRootLv1); //链接到节点pRoot下
pRootLv1->SetAttribute("index", index);
pRootLv1->SetAttribute("type", 0);
TiXmlElement
*pRootLv21 = new TiXmlElement("name");
pRootLv1->LinkEndChild(pRootLv21);
TiXmlText *pText1 =
new TiXmlText(name.toStdString().c_str());
pRootLv21->LinkEndChild(pText1);
TiXmlElement
*pRootLv22 = new TiXmlElement("age");
pRootLv1->LinkEndChild(pRootLv22);
TiXmlText *pText2 =
new TiXmlText(QString::number(age).toStdString().c_str());
pRootLv22->LinkEndChild(pText2);
}
void MainWindow::addStudent(TiXmlElement *pRoot)
{
TiXmlElement *pRootLv1
= new TiXmlElement("Students"); //创建一个节点
pRoot->LinkEndChild(pRootLv1); //链接到节点pRoot下
int index = 0;
addStudentData(pRootLv1, index, "张三", 18, 90.1);
index++;
addStudentData(pRootLv1, index, "李四", 19, 89.5);
index++;
addStudentData(pRootLv1, index, "王五", 17, 93);
}
void MainWindow::addStudentData(TiXmlElement *pRoot, int index,
QString name, int age, double score)
{
TiXmlElement *pRootLv1
= new TiXmlElement("Student"); //创建一个节点
pRoot->LinkEndChild(pRootLv1); //链接到节点pRoot下
pRootLv1->SetAttribute("index", index);
pRootLv1->SetAttribute("type",
1);
TiXmlElement
*pRootLv21 = new TiXmlElement("name");
pRootLv1->LinkEndChild(pRootLv21);
TiXmlText *pText1 =
new TiXmlText(name.toStdString().c_str());
pRootLv21->LinkEndChild(pText1);
TiXmlElement
*pRootLv22 = new TiXmlElement("age");
pRootLv1->LinkEndChild(pRootLv22);
TiXmlText *pText2 =
new TiXmlText(QString::number(age).toStdString().c_str());
pRootLv22->LinkEndChild(pText2);
TiXmlElement
*pRootLv23 = new TiXmlElement("score");
pRootLv1->LinkEndChild(pRootLv23);
TiXmlText *pText3 =
new TiXmlText(QString::number(score).toStdString().c_str());
pRootLv23->LinkEndChild(pText3);
}
四、遍历
void MainWindow::showData()
{
TiXmlDocument*
pDocument = new TiXmlDocument();
if(!pDocument->LoadFile(m_fileName.toStdString().c_str())) //加载xml,test.xml不存在时
{
qDebug() <<
pDocument->ErrorDesc();
return;
}
//获取根节点值
TiXmlElement* pRootLv1
= pDocument->RootElement();
const char* elemValue
= pRootLv1->Value();
if(strcmp(elemValue,
"Person") != 0)
{
qDebug() <<
"Person error";
return;
}
for(TiXmlElement*
pRootLv2 = pRootLv1->FirstChildElement();
pRootLv2 !=
nullptr; pRootLv2 = pRootLv2->NextSiblingElement())
{
const char*
elemValue = pRootLv2->Value();
if(strcmp(elemValue, "Teachers") == 0)
{
for(TiXmlElement* pRootLv3 = pRootLv2->FirstChildElement();
pRootLv3 != nullptr; pRootLv3 = pRootLv3->NextSiblingElement())
{
elemValue
= pRootLv3->Value();
if(strcmp(elemValue, "Teacher") == 0)
{
int
index = 0;
//读取int属性用pRootLv4->QueryIntAttribute("属性名称",
&value);
//读取double属性用pRootLv4->QueryDoubleAttribute("属性名称",
&value);
//读取字符串属性用value =
sublistElem->Attribute("属性名称");
pRootLv3->QueryIntAttribute("index", &index);
//读text
TiXmlElement* pRootLv4 =
pRootLv3->FirstChildElement("name");
QString name = pRootLv4->FirstChild()->ToText()->Value();
pRootLv4 = pRootLv3->FirstChildElement("age");
QString ageStr = pRootLv4->FirstChild()->ToText()->Value();
int
age = ageStr.toInt();
qDebug() << "编号:" << index << " 姓名:" <<
name << " 年龄:" << age;
}
}
}
else
if(strcmp(elemValue, "Students") == 0)
{
for(TiXmlElement* pRootLv3 = pRootLv2->FirstChildElement();
pRootLv3 != nullptr; pRootLv3 = pRootLv3->NextSiblingElement())
{
elemValue
= pRootLv3->Value();
if(strcmp(elemValue, "Student") == 0)
{
int
index = 0;
pRootLv3->QueryIntAttribute("index", &index);
//读text
TiXmlElement* pRootLv4 =
pRootLv3->FirstChildElement("name");
QString name = pRootLv4->FirstChild()->ToText()->Value();
pRootLv4 = pRootLv3->FirstChildElement("age");
QString ageStr = pRootLv4->FirstChild()->ToText()->Value();
int
age = ageStr.toInt();
pRootLv4 = pRootLv3->FirstChildElement("score");
QString scoreStr = pRootLv4->FirstChild()->ToText()->Value();
double
score = ageStr.toDouble();
qDebug() << "编号:" << index << " 姓名:" <<
name << " 年龄:" << age << " 成绩:" << score;
}
}
}
}
}
五、修改
void MainWindow::changeData()
{
QString nameC = "王老师";
int ageC = 25;
TiXmlDocument*
pDocument = new TiXmlDocument();
if(!pDocument->LoadFile(m_fileName.toStdString().c_str())) //加载xml,test.xml不存在时
{
qDebug() <<
pDocument->ErrorDesc();
return;
}
//获取根节点值
TiXmlElement* pRootLv1
= pDocument->RootElement();
const char* elemValue
= pRootLv1->Value();
if(strcmp(elemValue,
"Person") != 0)
{
qDebug() <<
"Person error";
return;
}
for(TiXmlElement*
pRootLv2 = pRootLv1->FirstChildElement();
pRootLv2 !=
nullptr; pRootLv2 = pRootLv2->NextSiblingElement())
{
const char*
elemValue = pRootLv2->Value();
if(strcmp(elemValue, "Teachers") == 0)
{
for(TiXmlElement* pRootLv3 = pRootLv2->FirstChildElement();
pRootLv3 != nullptr; pRootLv3 = pRootLv3->NextSiblingElement())
{
elemValue
= pRootLv3->Value();
if(strcmp(elemValue, "Teacher") == 0)
{
//修改属性
// int
index = 0;
// pRootLv3->SetAttribute("index",
QString::number(index).toStdString().c_str());
TiXmlElement* pRootLv4 = pRootLv3->FirstChildElement("name");
QString name = pRootLv4->FirstChild()->ToText()->Value();
if(name == nameC)
{
//修改text
pRootLv4 = pRootLv3->FirstChildElement("age");
pRootLv4->FirstChild()->SetValue(QString::number(ageC).toStdString().c_str());
}
}
}
}
}
pDocument->SaveFile();
}
六、删除
void MainWindow::deleteData()
{
QString nameC = "王老师";
TiXmlDocument*
pDocument = new TiXmlDocument();
if(!pDocument->LoadFile(m_fileName.toStdString().c_str())) //加载xml,test.xml不存在时
{
qDebug() <<
pDocument->ErrorDesc();
return;
}
//获取根节点值
TiXmlElement* pRootLv1
= pDocument->RootElement();
const char* elemValue
= pRootLv1->Value();
if(strcmp(elemValue,
"Person") != 0)
{
qDebug() <<
"Person error";
return;
}
for(TiXmlElement*
pRootLv2 = pRootLv1->FirstChildElement();
pRootLv2 !=
nullptr; pRootLv2 = pRootLv2->NextSiblingElement())
{
const char*
elemValue = pRootLv2->Value();
if(strcmp(elemValue, "Teachers") == 0)
{
for(TiXmlElement* pRootLv3 =
pRootLv2->FirstChildElement();
pRootLv3 != nullptr; pRootLv3 = pRootLv3->NextSiblingElement())
{
elemValue
= pRootLv3->Value();
if(strcmp(elemValue, "Teacher") == 0)
{
TiXmlElement* pRootLv4 =
pRootLv3->FirstChildElement("name");
QString name = pRootLv4->FirstChild()->ToText()->Value();
if(name == nameC)
{
//删除子节点
pRootLv2->RemoveChild(pRootLv3);
}
}
}
}
}
pDocument->SaveFile();
}
深圳市优飞迪科技有限公司成立于2010年,是一家专注于产品开发平台解决方案与物联网技术开发的国家级高新技术企业。
十多年来,优飞迪科技在数字孪生、工业软件尤其仿真技术、物联网技术开发等领域积累了丰富的经验,并在这些领域拥有数十项独立自主的知识产权。同时,优飞迪科技也与国际和国内的主要头部工业软件厂商建立了战略合作关系,能够为客户提供完整的产品开发平台解决方案。
优飞迪科技技术团队实力雄厚,主要成员均来自于国内外顶尖学府、并在相关领域有丰富的工作经验,能为客户提供“全心U+端到端服务”。