《轉》 在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,將他們拷到project文件夾里面。


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.略微復雜一些的樣例

以下這個樣例的場景更可能在project中遇到,就是在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 ) :獲得下一個節點。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM