读取和设置xml配置文件是最常用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,因为它的API接口和Java的十分类似,面向对象性很好。
TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。
如下是一个XML片段:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<Persons>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<Person ID="1">
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<name>周星星</name>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<age>20</age>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</Person>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<Person ID="2">
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<name>白晶晶</name>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<age>18</age>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</Person>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</Persons>
在TinyXML中,根据XML的各种元素来定义了一些类:
TiXmlBase:整个TinyXML模型的基类。
TiXmlAttribute:对应于XML中的元素的属性。
TiXmlNode:对应于DOM结构中的节点。
TiXmlComment:对应于XML中的注释
TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。
TiXmlDocument:对应于XML的整个文档。
TiXmlElement:对应于XML的元素。
TiXmlText:对应于XML的文字部分
TiXmlUnknown:对应于XML的未知部分。
TiXmlHandler:定义了针对XML的一些操作。
TinyXML是个解析库,主要由DOM模型类(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作类(TiXmlHandler)构成。它由两个头文件(.h文件)和四个CPP文件(.cpp文件)构成,用的时候,只要将(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)导入工程就可以用它的东西了。如果需要,可以将它做成自己的DLL来调用。举个例子就可以说明一切。。。
对应的XML文件:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<Persons>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<Person ID="1">
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<name>phinecos</name>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<age>22</age>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</Person>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
</Persons>
读写XML文件的程序代码:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include <iostream>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "tinyxml.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include "tinystr.h"
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include <string>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include <windows.h>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
#include <atlstr.h>
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
using namespace std;
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CString GetAppPath()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//获取应用程序根目录
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TCHAR modulePath[MAX_PATH];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
GetModuleFileName(NULL, modulePath, MAX_PATH);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CString strModulePath(modulePath);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return strModulePath;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
bool CreateXmlFile(string& szFileName)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建一个XML的文档对象。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlDocument *myDocument = new TiXmlDocument();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建一个根元素并连接。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlElement *RootElement = new TiXmlElement("Persons");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
myDocument->LinkEndChild(RootElement);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建一个Person元素并连接。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlElement *PersonElement = new TiXmlElement("Person");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
RootElement->LinkEndChild(PersonElement);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置Person元素的属性。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
PersonElement->SetAttribute("ID", "1");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建name元素、age元素并连接。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlElement *NameElement = new TiXmlElement("name");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlElement *AgeElement = new TiXmlElement("age");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
PersonElement->LinkEndChild(NameElement);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
PersonElement->LinkEndChild(AgeElement);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置name元素和age元素的内容并连接。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlText *NameContent = new TiXmlText("周星星");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlText *AgeContent = new TiXmlText("22");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
NameElement->LinkEndChild(NameContent);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
AgeElement->LinkEndChild(AgeContent);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CString appPath = GetAppPath();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string seperator = "\\";
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
myDocument->SaveFile(fullPath.c_str());//保存到文件
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
catch (string& e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return false;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return true;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
bool ReadXmlFile(string& szFileName)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{//读取Xml文件,并遍历
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CString appPath = GetAppPath();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string seperator = "\\";
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//创建一个XML的文档对象。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
myDocument->LoadFile();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//获得根元素,即Persons。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlElement *RootElement = myDocument->RootElement();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//输出根元素名称,即输出Persons。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cout << RootElement->Value() << endl;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//获得第一个Person节点。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//获得第一个Person的name节点和age节点和ID属性。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlElement *NameElement = FirstPerson->FirstChildElement();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cout << NameElement->FirstChild()->Value() << endl;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cout << AgeElement->FirstChild()->Value() << endl;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
cout << IDAttribute->Value()<< endl;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
catch (string& e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return false;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return true;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string fileName = "info.xml";
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
CreateXmlFile(fileName);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ReadXmlFile(fileName);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
作者:洞庭散人