天天看點

C++ XML解析之TinyXML篇

最近使用TinyXML進行C++ XML解析,感覺使用起來比較簡單,很容易上手,本文給出一個使用TinyXML進行XML解析的簡單例子,很多複雜的應用都可以基于本例子的方法來完成。以後的文章裡會講解使用Xerces進行C++ XML解析的例子,希望大家一起交流。

TinyXML是一個開源的解析XML的解析庫,能夠用于C++,能夠在Windows或Linux中編譯。這個解析庫的模型通過解析XML檔案,然後在記憶體中生成DOM模型,進而讓我們很友善的周遊這棵XML樹。

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

    首先從網上下載下傳TinyXML的庫,檔案夾的名字是TinyXpath,在工程裡做如下配置:

    在附加包含路徑裡添加:你的tinyxpath路徑/tinyxpath/include

    在附加庫路徑裡添加:你的tinyxpath路徑/tinyxpath/lib

    在對象/庫路徑裡添加:tinyxpathd.lib,如果使用release版本,則是tinyxpath.lib。

    另外,由于我開發的項目是多線程的,是以設定了多線程的環境,是以使用TinyXML沒有出現問題。本人将TinyXML寫在一個單獨的C++工程進行測試,發現如果不設定多線程的環境,會出現連結錯誤。我覺得原因可能是TinyXML使用了多線程環境,是以需要設定多線程的環境。在工程/設定下的C/C++頁籤中,選擇Code Generation,在Use run-time library中選擇Debug MultiThreaed DLL即可。

    本例的XML檔案Students.xml如下:

<Class name="計算機軟體班">

    <Students>

        <student name="張三" studentNo="13031001" sex="男" age="22">

            <phone>88208888</phone>

            <address>西安市太白南路二号</address>

        </student>

        <student name="李四" studentNo="13031002" sex="男" age="20">

            <phone>88206666</phone>

            <address>西安市光華路</address>

    </Students>

</Class>

    程式代碼XmlParseExample.cpp如下所示:

#include <iostream>

#include <string>

#include <tinyxml.h>

  using std::string;

  int main()

{

  TiXmlDocument* myDocument = new TiXmlDocument();

  myDocument->LoadFile("Students.xml");

  TiXmlElement* rootElement = myDocument->RootElement();  //Class

  TiXmlElement* studentsElement = rootElement->FirstChildElement();  //Students

  TiXmlElement* studentElement = studentsElement->FirstChildElement();  //Students

  while ( studentElement ) {

    TiXmlAttribute* attributeOfStudent = studentElement->FirstAttribute();  //獲得student的name屬性

    while ( attributeOfStudent ) {

      std::cout << attributeOfStudent->Name() << " : " << attributeOfStudent->Value() << std::endl;

      attributeOfStudent = attributeOfStudent->Next();

    }

    TiXmlElement* phoneElement = studentElement->FirstChildElement();//獲得student的phone元素

    std::cout << "phone" << " : " << phoneElement->GetText() << std::endl;

    TiXmlElement* addressElement = phoneElement->NextSiblingElement();

    std::cout << "address" << " : " << phoneElement->GetText() << std::endl;

    studentElement = studentElement->NextSiblingElement();

  }

  return 0;

}

    程式運作結果如下:

name : 張三

studentNo : 13031001

sex : 男

age : 22

phone : 88208888

address : 88208888

name : 李四

studentNo : 13031002

age : 20

phone : 88206666

address : 88206666

    本例中使用的是對xml檔案進行解析,很容易掌握,但是很多開發人員不知道如何對xml 字元流(非xml檔案)進行解析,我看了TinyXML提供的源代碼,裡面可以使用如下方法對xml流解析。對應于上例,代碼如下:

string xmlString = 

            "<Class name=\"計算機軟體班\">\

              <Students>\

                <student name=\"張三\" studentNo=\"13031001\" sex=\"男\" age=\"22\">\

                  <phone>88208888</phone>\

                  <address>西安市太白南路二号</address>\

                </student>\

                <student name=\"李四\" studentNo=\"13031002\" sex=\"男\" age=\"20\">\

                  <phone>88206666</phone>\

                  <address>西安市光華路</address>\

              </Students>\

            </Class>";

  myDocument->Parse(xmlString.c_str());

    使用Parse函數就可以解析XML字元流了,這是很多開發者不太熟悉的情況。

    如果開發者開發特定應用,就可以使用上述類似方法,可能不需要完全處理每一個屬性,比如可以對屬性名進行判斷,隻處理自己需要的屬性,或者自己需要的xml元素。還可以使用TinyXML的方法建立xml元素和xml屬性,或者設定xml元素和屬性對應的值,等等,如果讀者想要類似的例子,可以留言寫出。

     下面介紹TinyXML的一些類。在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的一些操作。

     本文轉自panpan3210 51CTO部落格,原文連結:http://blog.51cto.com/panpan/104961,如需轉載請自行聯系原作者