如何使用C++解析XML文件?

【本来是想明天在写的,拒绝拖延,写完再睡,干】
tinyxml库下载地址:
/

将下图中红框内的文件,导入到我们的工程中

创建我们自己的工程文件,我的是main.cpp(上图)
工程代码如下:

#include "tinyxml.h"
#include <iostream>
//这是似乎没啥用,我注释掉,代码依然可以运行
#include <cstring>
using namespace std;void printTestXml() {int i = 1;//把xml文件所在绝对路径赋值给string变量string filefullPath = "D:/Code/tinyxml/conf/test.xml";//创建文件对象  TiXmlDocument * myDocument = new TiXmlDocument(filefullPath.c_str());//加载文件myDocument->LoadFile();//获取文件根节点TiXmlElement* RootElement = myDocument->RootElement();TiXmlAttribute* scAttr = RootElement->FirstAttribute();//输出根节点的名称cout << RootElement->Value() << " ";//输出根节点的属性cout << scAttr->Name() << " : " << scAttr->Value() << endl;for (TiXmlElement* classElement = RootElement->FirstChildElement();NULL != classElement;classElement = classElement->NextSiblingElement()){//输出class元素的属性TiXmlAttribute* CAttr = classElement->FirstAttribute();while (CAttr != NULL){cout << "class " <<CAttr->Name() << ":" << CAttr->Value();CAttr = CAttr->Next();}cout << endl;cout << "=====================================\n";//输出student的属性和内容for (TiXmlElement* stuElement = classElement->FirstChildElement();NULL != stuElement;stuElement = stuElement->NextSiblingElement()){//输出student的属性cout << stuElement->Value() << "NO." << i << " ";i += 1;TiXmlAttribute* pAttr = stuElement->FirstAttribute();while (NULL != pAttr){cout << pAttr->Name() << ":" << pAttr->Value() << " ";pAttr = pAttr->Next();}cout << endl;//输出student的内容for (TiXmlElement* sonElement = stuElement->FirstChildElement();NULL != sonElement;sonElement = sonElement->NextSiblingElement()){cout << sonElement->Value() << ":";cout << sonElement->FirstChild()->Value() << endl;}if (NULL != stuElement->NextSiblingElement()){cout << endl;}}i = 1;cout << "--------------------------------------\n";cout << endl;}
}int main()
{printTestXml();system("pause");return 0;
}

我的xml:

<?xml version="1.0" encoding="UTF-8"?>
<School name="NO.1School"><Class name = "C++">  <Student name="tinyxml" number="123">  <email>tinyxml@163.com</email>  <address>CN</address>           </Student>  <Student name="jsoncpp" number="456">  <email>jsoncpp@gmail.com</email>  <address>US</address>           </Student></Class> <Class name = "java">  <Student name="tiny" number="12">  <email>tiny@163.com</email>  <address>china</address>           </Student>  <Student name="json" number="45">  <email>json@gmail.com</email>  <address>US</address>           </Student></Class> 
</School>

解析结果:

如何使用C++解析XML文件?

【本来是想明天在写的,拒绝拖延,写完再睡,干】
tinyxml库下载地址:
/

将下图中红框内的文件,导入到我们的工程中

创建我们自己的工程文件,我的是main.cpp(上图)
工程代码如下:

#include "tinyxml.h"
#include <iostream>
//这是似乎没啥用,我注释掉,代码依然可以运行
#include <cstring>
using namespace std;void printTestXml() {int i = 1;//把xml文件所在绝对路径赋值给string变量string filefullPath = "D:/Code/tinyxml/conf/test.xml";//创建文件对象  TiXmlDocument * myDocument = new TiXmlDocument(filefullPath.c_str());//加载文件myDocument->LoadFile();//获取文件根节点TiXmlElement* RootElement = myDocument->RootElement();TiXmlAttribute* scAttr = RootElement->FirstAttribute();//输出根节点的名称cout << RootElement->Value() << " ";//输出根节点的属性cout << scAttr->Name() << " : " << scAttr->Value() << endl;for (TiXmlElement* classElement = RootElement->FirstChildElement();NULL != classElement;classElement = classElement->NextSiblingElement()){//输出class元素的属性TiXmlAttribute* CAttr = classElement->FirstAttribute();while (CAttr != NULL){cout << "class " <<CAttr->Name() << ":" << CAttr->Value();CAttr = CAttr->Next();}cout << endl;cout << "=====================================\n";//输出student的属性和内容for (TiXmlElement* stuElement = classElement->FirstChildElement();NULL != stuElement;stuElement = stuElement->NextSiblingElement()){//输出student的属性cout << stuElement->Value() << "NO." << i << " ";i += 1;TiXmlAttribute* pAttr = stuElement->FirstAttribute();while (NULL != pAttr){cout << pAttr->Name() << ":" << pAttr->Value() << " ";pAttr = pAttr->Next();}cout << endl;//输出student的内容for (TiXmlElement* sonElement = stuElement->FirstChildElement();NULL != sonElement;sonElement = sonElement->NextSiblingElement()){cout << sonElement->Value() << ":";cout << sonElement->FirstChild()->Value() << endl;}if (NULL != stuElement->NextSiblingElement()){cout << endl;}}i = 1;cout << "--------------------------------------\n";cout << endl;}
}int main()
{printTestXml();system("pause");return 0;
}

我的xml:

<?xml version="1.0" encoding="UTF-8"?>
<School name="NO.1School"><Class name = "C++">  <Student name="tinyxml" number="123">  <email>tinyxml@163.com</email>  <address>CN</address>           </Student>  <Student name="jsoncpp" number="456">  <email>jsoncpp@gmail.com</email>  <address>US</address>           </Student></Class> <Class name = "java">  <Student name="tiny" number="12">  <email>tiny@163.com</email>  <address>china</address>           </Student>  <Student name="json" number="45">  <email>json@gmail.com</email>  <address>US</address>           </Student></Class> 
</School>

解析结果: