C++中解析XML tinyXML2

  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>  

  1. </scene>  
  2. 上述为XML 文俭
  3. 下述为解析
    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.     example2();  
    29.     return 0;  
    30. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值