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

你可能感兴趣的:(C++中解析XML tinyXML2)