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();
}
王老師的信息被刪除

工程師必備
- 項目客服
- 培訓客服
- 平臺客服
TOP




















