For a simple summary of the use of TinyXML: to parse XML data

Source: Internet
Author: User
After using the TinyXML, think this thing is really good, so will use the method to sit down to sum up and share with you.
The parsing library is in the Open source Web site ( http://sourceforge.netOn the download, also available in this blog download (download tinyxml)
TinyXML is an open source analytic library of parsing XML that can be used in C + + to compile in Windows or Linux. The model of the parsing library makes it easy to traverse the XML tree by parsing the XML file and then generating the DOM model in memory.
Note: The DOM model is the Document Object model, which divides the entire document into elements (such as books, chapters, sections, paragraphs, and so on), and uses a tree structure to represent the sequential relationships between these elements and nested inclusion relationships (the tree-like model is easily understood by readers who understand the HTML language).
The following is an XML fragment:
<Persons>
<person id= "1" >
<name> Week star </name>
<age>20</age>
</Person>
<person id= "2" >
<name> Bai Jingjing </name>
<age>18</age>
</Person>
</Persons>
In TinyXML, some classes are defined according to the various elements of XML:
Tixmlbase: The base class for the entire TinyXML model.
Tixmlattribute: attributes that correspond to elements in the XML.
Tixmlnode: Corresponds to nodes in the DOM structure.
Tixmlcomment: Corresponds to annotations in XML.
Tixmldeclaration: Corresponds to the Declaration section in XML, that is, <. Versiong= "1.0"?>.
Tixmldocument: The entire document that corresponds to XML.
Tixmlelement: An element that corresponds to XML.
Tixmltext: Corresponds to the text portion of the XML.
Tixmlunknown: Corresponds to an unknown part of XML.
Tixmlhandler: Defines some of the actions for XML.
So how do we use these classes and their methods to manipulate our XML? Please look below.
Read XML (assuming that the content in our XML document is the same as the XML content above)
Creates a document object for XML.
Tixmldocument *mydocument = new Tixmldocument ("Fill in your XML filename");
Mydocument->loadfile ();
Gets the root element, that is, the persons.
Tixmlelement *rootelement = Mydocument.rootelement ();
Outputs the root element name, which is the output persons.
cout << rootelement->value () << Endl;
Gets the first person node.
Tixmlelement *firstperson = Rootelement->firstchildelement ();
Gets the name node and age node and id attribute of the first person.
Tixmlelement *nameelement = Firstperson->firstchildelement ();
Tixmlelement *ageelement = Nameelement->nextsiblingelement ();
Tixmlattribute *idattribute = Firstperson->firstattribute ();
Outputs the name of the first person, the week star, the age content, which is the 20;id property, that is, 1.
cout << nameelement->firstchild ()->value << Endl;
cout << ageelement->firstchild ()->value << Endl;
cout << idattribute->value () << Endl;

See, reading XML is not very simple, and Java XML parsing library is very similar to the name of the change.
second, generate XML content
Creates a document object for XML.
Tixmldocument *mydocument = new Tixmldocument ();
Creates a root element and joins it.
Tixmlelement *rootelement = new Tixmlelement ("Persons");
Mydocument->linkendchild (rootelement);
Creates a person element and joins it.
Tixmlelement *personelement = new Tixmlelement ("person");
Rootelement->linkendchild (personelement);
Sets the properties of the person element.
Personelement->setattribute ("ID", "1");
Creates a name element, an age element, and joins.
Tixmlelement *nameelement = new Tixmlelement ("name");
Tixmlelement *ageelement = new Tixmlelement ("Age");
Personelement->linkendchild (nameelement);
Personelement->linkendchild (ageelement);
Sets the contents of the name element and the age element and joins it.
Tixmltext *namecontent = new Tixmltext ("Week Star");
Tixmltext *agecontent = new Tixmltext ("20");
Nameelement->linkendchild (namecontent);
Ageelement->linkendchild (agecontent);
Save to File
Mydocument->savefile ("XML filename to save");
This creates an XML file such as the following:
<Persons>
<person id= "1" >
<name> Week star </name>
<age>20</age>
</Person>
</Persons>

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.