1. 程式人生 > >在C++中使用TinyXML2解析xml

在C++中使用TinyXML2解析xml

讀取和設定xml配置檔案是最常用的操作,試用了幾個C++的XML解析器,個人感覺TinyXML是使用起來最舒服的,因為它的API介面和Java的十分類似,面向物件性很好。
      TinyXML是一個開源的解析XML的解析庫,能夠用於C++,能夠在Windows或Linux中編譯。這個解析庫的模型通過解析XML檔案,然後在記憶體中生成DOM模型,從而讓我們很方便的遍歷這棵XML樹。
      DOM模型即文件物件模型,是將整個文件分成多個元素(如書、章、節、段等),並利用樹型結構表示這些元素之間的順序關係以及巢狀包含關係。  

      不過官方的文件並不是很完善,例子更是不知所云...然後就有了下面的內容。

     這裡用的是TinyXML2,相比於TinyXML1,它更小,更輕量,記憶體的使用也更加有效。

    1.配置TinyXML2

    去這裡把專案弄下來,然後解壓,我們之需要裡面的tinyxml2.h和tinyxml2.cpp,將他們拷到工程目錄裡面。

 

2.HelloWorld

在專案中建立test.xml,內容如下:

 

[html]
  view plain copy  
  1. <?xml version="1.0"?>  
  2. <Hello>World</Hello>  

建立main.cpp

 

 

[cpp]  view plain copy  
  1. #include <iostream>
      
  2. #include"tinyxml2.h"  
  3. using namespace std;  
  4. using namespace tinyxml2;  
  5. void example1()  
  6. {  
  7.     XMLDocument doc;  
  8.     doc.LoadFile("test.xml");  
  9.     const char* content= doc.FirstChildElement( "Hello" )->GetText();  
  10.     printf( "Hello,%s", content );  
  11. }  
  12.   
  13. int main()  
  14. {  
  15.     example1();  
  16.     return 0;  
  17. }  
編譯執行:

 


3.稍微複雜一些的例子

下面這個例子的場景更可能在工程中遇到,就是在XML中儲存一些資料,然後由程式來呼叫。

 

[html]  view plain copy  
  1. <?xml version="1.0"?>  
  2. <scene name="Depth">  
  3.     <node type="camera">  
  4.         <eye>0 10 10</eye>  
  5.         <front>0 0 -1</front>  
  6.         <refUp>0 1 0</refUp>  
  7.         <fov>90</fov>  
  8.     </node>  
  9.     <node type="Sphere">  
  10.         <center>0 10 -10</center>  
  11.         <radius>10</radius>  
  12.     </node>  
  13.     <node type="Plane">  
  14.         <direction>0 10 -10</direction>  
  15.         <distance>10</distance>  
  16.     </node>  
  17. </scene>  

[cpp]  view plain copy  
  1. #include <iostream>  
  2. #include"tinyxml2.h"  
  3. using namespace std;  
  4. using namespace tinyxml2;  
  5. void example2()  
  6. {  
  7.     XMLDocument doc;  
  8.     doc.LoadFile("test.xml");  
  9.     XMLElement *scene=doc.RootElement();  
  10.     XMLElement *surface=scene->FirstChildElement("node");  
  11.     while (surface)  
  12.     {  
  13.         XMLElement *surfaceChild=surface->FirstChildElement();  
  14.         const char* content;  
  15.         const XMLAttribute *attributeOfSurface = surface->FirstAttribute();  
  16.         cout<< attributeOfSurface->Name() << ":" << attributeOfSurface->Value() << endl;  
  17.         while(surfaceChild)  
  18.         {  
  19.             content=surfaceChild->GetText();  
  20.             surfaceChild=surfaceChild->NextSiblingElement();  
  21.             cout<<content<<endl;  
  22.         }  
  23.         surface=surface->NextSiblingElement();  
  24.     }  
  25. }  
  26. int main()  
  27. {  
  28.     example1();  
  29.     return 0;  
  30. }  

執行結果

 

解釋一下幾個函式:

FirstChildElement(const char* value=0):獲取第一個值為value的子節點,value預設值為空,則返回第一個子節點。

RootElement():獲取根節點,相當於FirstChildElement的空引數版本;

const XMLAttribute* FirstAttribute() const:獲取第一個屬性值;

XMLHandle NextSiblingElement( const char* _value=0 ) :獲得下一個節點。

再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智慧的隊伍中來!https://www.cnblogs.com/captainbed