1. Brief Introduction:
I tried the 3rd library in tinyxml, rapidxml, and libxml2. The first two are relatively lightweight and libxml2 is very comprehensive and powerful.
Here are the advantages and disadvantages summarized on the Internet:
1. tinyxml: When the node content is empty and the gettext () method is used, an error occurs.
2. The issue of rapidxml encoding is not particularly good.
3. libxml2 has high requirements on memory release; otherwise, core dump is often encountered.
Tinyxml is an open-source parsing XML library that can be used in C ++ and compiled in Windows or Linux. The parsing library's model parses the XML file and then generates the DOM model in the memory, so that we can easily traverse this XML tree.
The Dom model is a Document Object Model. It divides a 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.
First, we will introduce some classes defined in tinyxml based on the various elements of XML:
Tixmlbase: The base class of the entire tinyxml model.
Tixmlattribute: attribute corresponding to the element in XML.
Tixmlnode: corresponds to a node 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 XML operations.
Tinyxml class diagram:
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"
Tixmlelement "bold"
Tixmltext "toy store! "
Tixmlelement "item" attributes: Priority = 2
Tixmltext "Do bills"
In tinyxml, when you use firstchild ("name") to find a node, the node that calls the firstchild function must have a parent-child relationship with the node to be searched ".
2. Implementation elements
The following is an XML snippet:
<Persons>
<Person id = "1">
<Name> Stephen Chow </Name>
<Age> 40 </age>
</Person>
<Person id = "2">
<Name> Bai Jingjing </Name>
<Age> 30 </age>
</Person>
</Persons>
1. Read XML
// 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, that is, 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 xingchi; age content, that is, 40; Id attribute, that is, 1.
Cout <nameelement-> firstchild ()-> value <Endl;
Cout <ageelement-> firstchild ()-> value <Endl;
Cout <idattribute-> value () <Endl;
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 ("Stephen Chow ");
Tixmltext * agecontent = new tixmltext ("40 ");
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> Stephen Chow </Name>
<Age> 40 </age>
</Person>
</Persons>
3. Give an example to illustrate common usage. Use the code snippets of my current project to describe them:
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
= 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. parse the following content
<? XML version = "1.0" encoding = "UTF-8"?>
<Request ID = "" type = "addeventhub">
<Item name = "touchscreen" multed = "16" 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 pack
There is the following XML buff content. How can we pack 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-> 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, "/> ");
XML can be used for packaging, but this complicated stuff is not used in a specific project, so it is directly concatenated with strings.
OK. It's very easy to use!