TinyXML Simple Use method

Source: Internet
Author: User

1. Brief Introduction:

Try the TINYXML,RAPIDXML,LIBXML2, such as the 3rd party library, the former two relatively lightweight, libxml2 very perfect, powerful.
This is the online summary of the advantages and disadvantages:
1, tinyxml error when using GetText () method when the content of the node is empty
2, Rapidxml coding problem is not particularly good
3, LIBXML2 to memory release requirements are high, or often encounter core dump.


TinyXML is an open source parsing XML parsing library that can be used in C + + and can be compiled in Windows or Linux. The model of this analytic library parses the XML file and then generates the DOM model in memory, which makes it easy to traverse the XML tree.
The DOM model, the Document object model, divides the entire document into elements such as books, chapters, sections, paragraphs, and so on, and uses a tree structure to represent the order relationships between these elements and the nested containment relationships.


First of all, in TinyXML, some classes are defined according to the various elements of XML:
Tixmlbase: The base class for the entire TinyXML model.
Tixmlattribute: A property that corresponds to an element in XML.
Tixmlnode: Corresponds to a node in the DOM structure.
Tixmlcomment: Corresponds to the comment in the XML.
Tixmldeclaration: Corresponds to the declaration part of XML, namely < Versiong= "1.0"?>.
Tixmldocument: The entire document that corresponds to XML.
Tixmlelement: The element that corresponds to the XML.
Tixmltext: Corresponds to the text portion of the XML.
Tixmlunknown: Corresponds to an unknown part of XML.
Tixmlhandler: Defines some operations for XML.

TinyXML class Diagram:



So how do these elements correspond? For example
<?xml version= "1.0" encoding= "Utf-8"?>
<!–our to Do List data–>
<ToDo>
<item priority= "1" > Go to the <bold>toy store!</bold></item>
<item priority= "2" > Do bills</item>
</ToDo>


Entire Object tree:


Tixmldocument "Demo.xml"
Tixmldeclaration version= "1.0" encoding= "Utf-8"?>
Tixmlcomment "Our To Do List data"
Tixmlelement "ToDo"
Tixmlelement "Item" attribtutes:priority = 1
Tixmltext "Go to the"
Tixmlelement "Bold"
Tixmltext "Toy store!"
Tixmlelement "Item" attributes:priority=2
Tixmltext "Do Bills"


In TinyXML, when you look up a node with FirstChild ("name"), the node that calls the FirstChild function must be a "parent-child relationship" with the node you are looking for.


2. Implement the use of elements


Here is an XML fragment:
<Persons>
<person id= "1" >
<name> Stephen Chow </name>
<age>40</age>
</Person>
<person id= "2" >
<name> Bai Jingjing </name>
<age>30</age>
</Person>
</Persons>


First, read the XML
Creates an XML Document object.
Tixmldocument *mydocument = new Tixmldocument ("Fill in your XML file name");
Mydocument->loadfile ();
Gets the root element, which is persons.
Tixmlelement *rootelement = Mydocument.rootelement ();
The output 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 the age node and ID properties of the first person.
Tixmlelement *nameelement = Firstperson->firstchildelement ();
Tixmlelement *ageelement = Nameelement->nextsiblingelement ();
Tixmlattribute *idattribute = Firstperson->firstattribute ();
Output the name of the first person, Stephen Chow; age content, which is the 40;id attribute, which is 1.
cout << nameelement->firstchild ()->value << Endl;
cout << ageelement->firstchild ()->value << Endl;
cout << idattribute->value () << Endl;

Ii. Generating XML content
Creates 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.
Tixmlelement *personelement = new Tixmlelement ("person");
Rootelement->linkendchild (personelement);
Sets the properties of the person element.
Personelement->setattribute ("ID", "1");
Create a name element, an age element, and connect.
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 connects.
Tixmltext *namecontent = new Tixmltext ("Stephen Chow Chi");
Tixmltext *agecontent = new Tixmltext ("40");
Nameelement->linkendchild (namecontent);
Ageelement->linkendchild (agecontent);
Save to File
Mydocument->savefile ("XML file name to save");
This creates an XML file such as the following:
<Persons>
<person id= "1" >
<name> Stephen Chow </name>
<age>40</age>
</Person>
</Persons>


3, illustrate the common usage, in my current project to replace the code snippet Description:
1, parse the following XML content
<?xml version= "1.0" encoding= "Utf-8"?>
<server IP = "Port =" "name =" "Version =" "/>


Core parsing code:
for (element = document. Firstchildelement (); Element Element
= document. Nextsiblingelement ()) {
while (sattr! = NULL) {
Name = Sattr->name ();
Value = Sattr->value ();
if (xmlstr_compare ("IP", name)) {
if (' 0 ' <= value[0] && value[0] <= ' 9 ') {
Bc.ip = xml_inet_addr (value);
} else {
Bc.ip = Ip_inaddr_any;
}
} else if (Xmlstr_compare ("Port", name)) {
Bc.port = atoi (value);
} else if (Xmlstr_compare ("name", name)) {
memcpy ((void*) Bc.name, value, 16);
} else if (Xmlstr_compare ("version", name)) {
memcpy ((void*) bc.version, value, 16);
}


sattr = Sattr->next ();
}
}


2, the following content analysis
<?xml version= "1.0" encoding= "Utf-8"?>
<request id= "" type= "Addeventhub" >
<item name= "touchscreen" multed = "All" width= "height=" "dpi=" "componentid=" 1 "/>
<item name= "Mouse" width= "" height= "" dpi= "" componentid= "2"/>
<item name= "Keyboard" componentid= "3"/>
<item name= "Dpad" componentid= "4"/>
<item name= "GamePad" componentid= "5"/>
<item name= "Trackball" componentid= "6"/>
</request>


Core parsing code:
while (sattr! = NULL) {
Name = Sattr->name ();
Value = Sattr->value ();
...
sattr = Sattr->next ();
}

Itemelement = Element->firstchildelement ();
while (itemelement! = NULL) {
int ItemNo =-1;
Name = Itemelement->gettext ();
Value = Itemelement->value ();

Itemattr = Itemelement->firstattribute ();
while (itemattr! = NULL) {
Name = Itemattr->name ();
Value = Itemattr->value ();

...

Itemattr = Itemattr->next ();
}
}


3. How to Bundle
The following XML buff content, how to bundle it?
<?xml version= "1.0" encoding= "Utf-8"?>
<server IP = "Port =" "name =" "Version =" "/>


XML_UDP_BC *XBC = (xml_udp_bc*) xml_param->param;


if (Xml_param->head_flag = = 1) {
p + = sprintf (P, xml_head);
}


p + = sprintf (p, "<server IP = \" ");
p + = sprintf (p, Xml_iptostr (XBC-&GT;IP));
p + = sprintf (p, "\" ");
p + = sprintf (p, "port = \"%d\ "", Xbc->port);
p + = sprintf (p, "name = \"%s\ "", xbc->name);
p + = sprintf (p, "version = \"%s\ "", xbc->version);
p + = sprintf (P, "/>");


You can use XML for a bundle, but it doesn't use the complex stuff in the project, so it's just a string stitch.

OK, simple to use a bit very simple!


TinyXML Simple Use method

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.