Qt使用TinyXML访问XML文件
版权声明:本文为CSDN博主「怎么追摩羯座」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Viciower/article/details/124848498
一、下载TinyXML
TinyXML download | SourceForge.net(链接:https://sourceforge.net/projects/tinyxml/ )
新建个Qt工程,下载TinyXML,把压缩包中的tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp添加到工程中
二、几个类
//文档类
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");
执行上面的代码生成如下的XML文件,第一行是声明;第二行是注释;
第三行是元素类,index是属性,name是文本
三、增加
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);
}
生成XML如下
四、遍历
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();
}
王老师的age被更改为25
六、删除
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();
}
王老师的信息被删除