TinyXML:一個優秀的C++ XML解析器

讀取和設置xml配置文件是最常用的操作,試用了幾個C++XML解析器,個人感覺TinyXML是使用起來最舒服的,因爲它的API接口和Java的十分類似,面向對象性很好。

TinyXML是一個開源的解析XML的解析庫,能夠用於C++,能夠在WindowsLinux中編譯。這個解析庫的模型通過解析XML文件,然後在內存中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。

DOM模型即文檔對象模型,是將整個文檔分成多個元素(如書、章、節、段等),並利用樹型結構表示這些元素之間的順序關係以及嵌套包含關係。

如下是一個XML片段:
   <Persons>
        <Person ID="1">
            <name>周星星</name>
            <age>20</age>
        </Person>
        <Person ID="2">
            <name>白晶晶</name>
            <age>18</age>
        </Person>
    </Persons>

  首先類結構圖:

 

 

  上圖錯了,TiXmlElement TiXmlAttributeSet 是關聯關係,而非依賴.

  TiXmlAttributeSet  TiXmlAttribute既關聯 又依賴

 

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模型類(TiXmlBaseTiXmlNodeTiXmlAttributeTiXmlCommentTiXmlDeclarationTiXmlElementTiXmlTextTiXmlUnknown)和操作類(TiXmlHandler)構成。它由兩個頭文件(.h文件)和四個CPP文件(.cpp文件)構成,用的時候,只要將(tinyxml.htinystr.htinystr.cpptinyxml.cpptinyxmlerror.cpptinyxmlparser.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);
}

附: 生成xml要領:

 TiXmlDocument       doc;
    TiXmlDeclaration * 
    decl = new TiXmlDeclaration("1.0","UTF-8","yes" );
    TiXmlElement * 
         element = new TiXmlElement( "Hello" );
    TiXmlText * 
                 text = new TiXmlText( "World" );
   

   element->LinkEndChild( text );


    doc.LinkEndChild( decl );
    doc.LinkEndChild( element );
    doc.SaveFile( "madeByHand.xml" );


原文地址:http://blog.csdn.net/weiqubo/article/details/6913260

發佈了5 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章