Use of TinyXML2

Source: Internet
Author: User
Tags xml parser

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.