From: http://www.cnblogs.com/freecoder/archive/2006/08/07/TinyXmlStapleA.html
Used this time Tinyxml I think this is really good, so I will share it with you.
The resolution library is on an open-source website ( Http://sourceforge.net ). This blog also provides download ( Download tinyxml )
Tinyxml Is an open-source Parsing XML DNS library, which can be used C ++ In Windows Or Linux . The model of this database is parsed.XML File, and then generate Dom Model, so that we can easily traverse this lesson XML Tree.
Note: The Dom model refers to the Document Object Model, which divides the entire document into multiple elements (such as books, chapters, sections, and segments ), the tree structure is used to represent the ordered relationship between these elements and the nested inclusion relationship (readers who understand the HTML language can easily understand this tree model ).
The following is an XML snippet:
<Persons>
<Person id = "1">
<Name> Zhou Xingxing </Name>
<Age> 20 </age>
</Person>
<Person id = "2">
<Name> Bai Jingjing </Name>
<Age> 18 </age>
</Person>
</Persons>
In tinyxml, some classes are defined based on various elements of XML:
Tixmlbase : The base class of the entire tinyxml model.
Tixmlattribute : Corresponds to the attributes of elements in XML.
Tixmlnode : Corresponds to nodes in the DOM structure.
Tixmlcomment : Corresponds to the comment in XML.
Tixmldeclaration : Corresponds to the declarative part in XML, that is, <? Versiong = "1.0"?>.
Tixmldocument : Corresponds to the entire XML document.
Tixmlelement : Elements corresponding to XML.
Tixmltext : Corresponds to the text section of XML.
Tixmlunknown : Corresponds to the unknown part of XML.
Tixmlhandler : Defines some operations for XML.
How can we use these classes and their methods to manipulate our XML? See the following.
1. Read XML (assume that the content in our XML document is the same as that in the preceding XML document)
// Create an XML Document Object .
Tixmldocument * mydocument = new tixmldocument ("fill in your xml file name ");
Mydocument-> LoadFile ();
// Obtain the root element, namely, persons.
Tixmlelement * rootelement = mydocument. rootelement ();
// Output the root element name, namely, the output persons.
Cout <rootelement-> value () <Endl;
// Obtain the first person node.
Tixmlelement * firstperson = rootelement-> firstchildelement ();
// Obtain the name and age nodes and ID attributes of the first person.
Tixmlelement * nameelement = firstperson-> firstchildelement ();
Tixmlelement * ageelement = nameelement-> nextsiblingelement ();
Tixmlattribute * idattriperson = firstperson-> firstattribute ();
// Output the name of the first person, that is, Zhou Xing; age, that is, 20; Id attribute, that is, 1.
Cout <nameelement-> firstchild ()-> value <Endl;
Cout <ageelement-> firstchild ()-> value <Endl;
Cout <idattribute-> value () <Endl;
Check whether it is very easy to read XML. It is very similar to the XML parsing library of Java, that is, the name has been changed.
Ii. Generate XML content
// Create an XML document object.
Tixmldocument * mydocument = new tixmldocument ();
// Create a root element and connect it.
Tixmlelement * rootelement = new tixmlelement ("persons ");
Mydocument-> linkendchild (rootelement );
// Create a person element and connect it.
Tixmlelement * personelement = new tixmlelement ("person ");
Rootelement-> linkendchild (personelement );
// Set the attributes of the person element.
Personelement-> setattribute ("ID", "1 ");
// Create and connect the name and age elements.
Tixmlelement * nameelement = new tixmlelement ("name ");
Tixmlelement * ageelement = new tixmlelement ("Age ");
Personelement-> linkendchild (nameelement );
Personelement-> linkendchild (ageelement );
// Set and connect the content of the name and age elements.
Tixmltext * namecontent = new tixmltext (" ");
Tixmltext * agecontent = new tixmltext ("20 ");
Nameelement-> linkendchild (namecontent );
Ageelement-> linkendchild (agecontent );
// Save to file
Mydocument-> SaveFile ("XML file name to be saved ");
In this way, the following XML file is created:
<Persons>
<Person id = "1">
<Name> Zhou Xingxing </Name>
<Age> 20 </age>
</Person>
</Persons>
Is it easy? Here, I just briefly introduced the use of tinyxml. You are welcome to ask questions and leave a message. I will try my best to answer your questions. [By lqbest]