[库][c++]tinyxml2使用小结

参考:http://blog.csdn.net/educast/article/details/12908455

1.配置TinyXML2

    去这里把项目弄下来,然后解压,我们之需要里面的tinyxml2.h和tinyxml2.cpp,将他们拷到工程目录里面。

 

2.HelloWorld

在项目中创建test.xml,内容如下:

 

[html] view plaincopy

 
 
  1. <?xml version="1.0"?>  
  2. <Hello>World</Hello>  

创建main.cpp

 

 

[cpp] view plaincopy

 
 
  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. }  

编译运行:

 

[库][c++]tinyxml2使用小结

3.稍微复杂一些的例子

下面这个例子的场景更可能在工程中遇到,就是在XML中存储一些数据,然后由程序来调用。

 

[html] view plaincopy

 
 
  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 plaincopy

 
 
  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. }  


运行结果

 

[库][c++]tinyxml2使用小结

解释一下几个函数:

FirstChildElement(const char* value=0):获取第一个值为value的子节点,value默认值为空,则返回第一个子节点。

RootElement():获取根节点,相当于FirstChildElement的空参数版本;

const XMLAttribute* FirstAttribute() const:获取第一个属性值;

XMLHandle NextSiblingElement( const char* _value=0 ) :获得下一个节点。

原文链接: https://www.cnblogs.com/lyggqm/p/7204472.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    [库][c++]tinyxml2使用小结

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/257084

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午10:35
下一篇 2023年2月14日 上午10:36

相关推荐