讀取和設置xml配置文件是最經常使用的操做,試用了幾個C++的XML解析器,我的感受TinyXML是使用起來最舒服的,由於它的API接口和Java的十分相似,面向對象性很好。ios
TinyXML是一個開源的解析XML的解析庫,可以用於C++,可以在Windows或Linux中編譯。這個解析庫的模型經過解析XML文件,而後在內存中生成DOM模型,從而讓咱們很方便的遍歷這棵XML樹。windows
DOM模型即文檔對象模型,是將整個文檔分紅多個元素(如書、章、節、段等),並利用樹型結構表示這些元素之間的順序關係以及嵌套包含關係。app
以下是一個XML片斷:
<Persons>
<Person ID="1">
<name>周星星</name>
<age>20</age>
</Person>
<Person ID="2">
<name>白晶晶</name>
<age>18</age>
</Person>
</Persons>
在TinyXML中,根據XML的各類元素來定義了一些類:spa
TiXmlBase:整個TinyXML模型的基類。xml
TiXmlAttribute:對應於XML中的元素的屬性。對象
TiXmlNode:對應於DOM結構中的節點。blog
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文件:
<Persons>
<Person ID="1">
<name>phinecos</name>
<age>22</age>
</Person>
</Persons>
讀寫XML文件的程序代碼:
#include <iostream>
#include "tinyxml.h"
#include "tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h>
using namespace std;
CString GetAppPath()
{//獲取應用程序根目錄
TCHAR modulePath[MAX_PATH];
GetModuleFileName(NULL, modulePath, MAX_PATH);
CString strModulePath(modulePath);
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
return strModulePath;
}
bool CreateXmlFile(string& szFileName)
{//建立xml文件,szFilePath爲文件保存的路徑,若建立成功返回true,不然false
try
{
//建立一個XML的文檔對象。
TiXmlDocument *myDocument = new TiXmlDocument();
//建立一個根元素並鏈接。
TiXmlElement *RootElement = new TiXmlElement("Persons");
myDocument->LinkEndChild(RootElement);
//建立一個Person元素並鏈接。
TiXmlElement *PersonElement = new TiXmlElement("Person");
RootElement->LinkEndChild(PersonElement);
//設置Person元素的屬性。
PersonElement->SetAttribute("ID", "1");
//建立name元素、age元素並鏈接。
TiXmlElement *NameElement = new TiXmlElement("name");
TiXmlElement *AgeElement = new TiXmlElement("age");
PersonElement->LinkEndChild(NameElement);
PersonElement->LinkEndChild(AgeElement);
//設置name元素和age元素的內容並鏈接。
TiXmlText *NameContent = new TiXmlText("周星星");
TiXmlText *AgeContent = new TiXmlText("22");
NameElement->LinkEndChild(NameContent);
AgeElement->LinkEndChild(AgeContent);
CString appPath = GetAppPath();
string seperator = "\\";
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
myDocument->SaveFile(fullPath.c_str());//保存到文件
}
catch (string& e)
{
return false;
}
return true;
}
bool ReadXmlFile(string& szFileName)
{//讀取Xml文件,並遍歷
try
{
CString appPath = GetAppPath();
string seperator = "\\";
string fullPath = appPath.GetBuffer(0) +seperator+szFileName;
//建立一個XML的文檔對象。
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
myDocument->LoadFile();
//得到根元素,即Persons。
TiXmlElement *RootElement = myDocument->RootElement();
//輸出根元素名稱,即輸出Persons。
cout << RootElement->Value() << endl;
//得到第一個Person節點。
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
//得到第一個Person的name節點和age節點和ID屬性。
TiXmlElement *NameElement = FirstPerson->FirstChildElement();
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
//輸出第一個Person的name內容,即周星星;age內容,即;ID屬性,即。
cout << NameElement->FirstChild()->Value() << endl;
cout << AgeElement->FirstChild()->Value() << endl;
cout << IDAttribute->Value()<< endl;
}
catch (string& e)
{
return false;
}
return true;
}
int main()
{
string fileName = "info.xml";
CreateXmlFile(fileName);
ReadXmlFile(fileName);
}