Library [C++]TINYXML2 Use Summary

Source: Internet
Author: User
Tags gettext

Reference: http://blog.csdn.net/educast/article/details/12908455

1. Configure TinyXML2

Go here to get the project down, then unzip, we need the inside of the Tinyxml2.h and Tinyxml2.cpp, will they cuff to the project directory.

2.HelloWorld

Create the Test.xml in the project as follows:

[HTML]View Plaincopy
    1. <? XML version="1.0"?>
    2. <hello>world</Hello>


Create 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. int main ()
  13. {
  14. Example1 ();
  15. return 0;
  16. }

Compile run:


3. A slightly more complex example

The scenario in the following example is more likely to be encountered in the project, which is to store some data in XML, which is then called by the program.

[HTML]View Plaincopy
  1. <? XML version="1.0"?>
  2. <scene name="Depth">
  3. <node type="Camera">
  4. <eye>0 ten</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. }


Run results

Explain some of the functions:

Firstchildelement (const char* value=0): Gets the first child node that is value, and the value default value is NULL, the first child node is returned.

RootElement (): Gets the root node, which is equivalent to the null parameter version of firstchildelement;

Const xmlattribute* FirstAttribute () const: Gets the value of the first property;

Xmlhandle nextsiblingelement (const char* _value=0): Gets the next node.

Library [C++]TINYXML2 Use Summary

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.