The recent project requires XML, and then a simple study of XML, in this simple description of the XML element parsing process, learning examples from the
http://blog.csdn.net/educast/article/details/12908455
Go here to get the XML parser file, we just need tinyxml2.h and tinyxml2.cpp, to copy them into the project directory.
Acquisition of 1.XML element content
Create a simple XML file
1 <? XML version= "1.0" ?> 2 < Hello > 3 World4</Hello>
Then write the program to get the XML element content.
1#include <iostream>2#include <fstream>3#include"tinyxml2.h"4 using namespaceTINYXML2;5 using namespacestd;6 7 voidexample1 ()8 { 9 XMLDocument Doc; TenDoc. LoadFile ("Test.xml"); One A Const Char* content= Doc. Firstchildelement ("Hello"),GetText (); -cout << Content <<Endl; - } the - intMain () - { - example1 (); + - return 0; +}
Note: Different writing formats in the XML file output different element content formats, such as the following:
2. Examples of complexity
1 <?XML version= "1.0"?> 2 <Scenename= "Depth"> 3 <nodetype= "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 <nodetype= "Sphere"> Ten <Center>0 10-10</Center> One <radius>10</radius> A </node> - <nodetype= "Plane"> - <direction>0 10-10</direction> the <Distance>10</Distance> - </node> - </Scene>
1#include <iostream>2#include <fstream>3#include"tinyxml2.h"4 using namespaceTINYXML2;5 using namespacestd;6 7#include <iostream>8#include"tinyxml2.h" 9 using namespacestd; Ten using namespaceTINYXML2; One voidexample2 () A { - XMLDocument Doc; -Doc. LoadFile ("Test.xml"); theXMLElement *scene=Doc. RootElement (); -XMLElement *surface=scene->firstchildelement ("node"); - while(surface) - { +XMLElement *surfacechild=surface->firstchildelement (); - Const Char*content; + ConstXmlAttribute *attributeofsurface = surface->FirstAttribute (); Acout<< Attributeofsurface->name () <<":"<< Attributeofsurface->value () <<Endl; at while(Surfacechild) - { -Content=surfacechild->GetText (); -Surfacechild=surfacechild->nextsiblingelement (); -cout<<content<<Endl; - } inSurface=surface->nextsiblingelement (); - } to } + intMain () - { the example2 (); * return 0; $}
Use of TinyXML2