西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
西西首頁 電腦軟件 安卓軟件 電腦游戲 安卓游戲 排行榜 專題合集

TinyXML解析器

v2.6.2 免費版
  • TinyXML解析器v2.6.2 免費版
  • 軟件大小:285KB
  • 更新時間:2013-08-01 09:17
  • 軟件語言:中文
  • 軟件廠商:
  • 軟件類別:國產(chǎn)軟件 / 免費軟件 / 編程輔助
  • 軟件等級:2級
  • 應(yīng)用平臺:WinAll, WinXP
  • 官方網(wǎng)站:暫無
  • 應(yīng)用備案:
好評:50%
壞評:50%

軟件介紹

讀取和設(shè)置xml配置文件是最常用的操作,試用了幾個C++的XML解析器,個人感覺TinyXML是使用起來最舒服的,因為它的API接口和Java的十分類似,面向?qū)ο笮院芎谩?br />TinyXML是一個開源的解析XML的解析庫,能夠用于C++,能夠在Windows或Linux中編譯。這個解析庫的模型通過解析XML文件,然后在內(nèi)存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。
DOM模型即文檔對象模型,是將整個文檔分成多個元素(如書、章、節(jié)、段等),并利用樹型結(jié)構(gòu)表示這些元素之間的順序關(guān)系以及嵌套包含關(guān)系。

如下是一個XML片段:

<Persons>
<Person ID="1">
<name>周星星</name>
<age>20</age>
</Person>
<Person ID="2">
<name>白晶晶</name>
<age>18</age>
</Person>
</Persons>

根據(jù)XML的各種元素來定義了一些類:

TiXmlBase:整個TinyXML模型的基類。
TiXmlAttribute:對應(yīng)于XML中的元素的屬性。
TiXmlNode:對應(yīng)于DOM結(jié)構(gòu)中的節(jié)點。
TiXmlComment:對應(yīng)于XML中的注釋
TiXmlDeclaration:對應(yīng)于XML中的申明部分,即<?versiong="1.0" ?>。
TiXmlDocument:對應(yīng)于XML的整個文檔。
TiXmlElement:對應(yīng)于XML的元素。
TiXmlText:對應(yīng)于XML的文字部分
TiXmlUnknown:對應(yīng)于XML的未知部分。
TiXmlHandler:定義了針對XML的一些操作。
TinyXML是個解析庫,主要由DOM模型類(TiXmlBase、TiXmlNode、TiXmlAttribute、TiXmlComment、TiXmlDeclaration、TiXmlElement、TiXmlText、TiXmlUnknown)和操作類(TiXmlHandler)構(gòu)成。它由兩個頭文件(.h文件)和四個CPP文件(.cpp文件)構(gòu)成,用的時候,只要將(tinyxml.h、tinystr.h、tinystr.cpp、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp)導(dǎo)入工程就可以用它的東西了。如果需要,可以將它做成自己的DLL來調(diào)用。舉個例子就可以說明一切。。。

對應(yīng)的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()
{//獲取應(yīng)用程序根目錄
TCHAR modulePath[MAX_PATH];
GetModuleFileName(NULL, modulePath, MAX_PATH);
CString strModulePath(modulePath);
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
return strModulePath;
}

bool CreateXmlFile(string& szFileName)
{//創(chuàng)建xml文件,szFilePath為文件保存的路徑,若創(chuàng)建成功返回true,否則false
try
{
//創(chuàng)建一個XML的文檔對象。
TiXmlDocument *myDocument = new TiXmlDocument();
//創(chuàng)建一個根元素并連接。
TiXmlElement *RootElement = new TiXmlElement("Persons");
myDocument->LinkEndChild(RootElement);
//創(chuàng)建一個Person元素并連接。
TiXmlElement *PersonElement = new TiXmlElement("Person");
RootElement->LinkEndChild(PersonElement);
//設(shè)置Person元素的屬性。
PersonElement->SetAttribute("ID", "1");
//創(chuàng)建name元素、age元素并連接。
TiXmlElement *NameElement = new TiXmlElement("name");
TiXmlElement *AgeElement = new TiXmlElement("age");
PersonElement->LinkEndChild(NameElement);
PersonElement->LinkEndChild(AgeElement);
//設(shè)置name元素和age元素的內(nèi)容并連接。
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;
//創(chuàng)建一個XML的文檔對象。
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
myDocument->LoadFile();
//獲得根元素,即Persons。
TiXmlElement *RootElement = myDocument->RootElement();
//輸出根元素名稱,即輸出Persons。
cout << RootElement->Value() << endl;
//獲得第一個Person節(jié)點。
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
//獲得第一個Person的name節(jié)點和age節(jié)點和ID屬性。
TiXmlElement *NameElement = FirstPerson->FirstChildElement();
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
//輸出第一個Person的name內(nèi)容,即周星星;age內(nèi)容,即;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);
}
軟件標(biāo)簽: XML

軟件截圖

TinyXML解析器 v2.6.2 免費版

其他版本下載

發(fā)表評論

昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
查看所有(0)條評論 > 字?jǐn)?shù): 0/500

TOP
軟件下載