Zhu Jincan
Source: http://blog.csdn.net/clever101
For XML files, my job is only to use configuration files and simple information files. Therefore, I do not like to use heavyweight XML parsers such as MSXML, in particular, parsing XML using MSXML involves complicated com type conversion, which is even more complicated. Therefore, I prefer to use the open-source tinyxml for parsing simple XML files.
First, let's introduce tinyxml. Tinyxml is a popular XML Parser Based on the DOM model. It is easy to use and small and exquisite, and is suitable for storing simple data and configuration files, object serialization and other data volume operations are not very large. The homepage is http://www.grinninglizard.com/tinyxml/. the latest version number is 2.5.3.
There are a lot of tutorials on tinyxml, but I don't think it is very easy (I feel that I didn't learn it after reading it ). No way. You just have to organize one article that suits you. If you are not suitable for others, you will be wise. I feel that XML files are essentially small databases. In other words, you should be able to perform any operations on the database on XML files. Generally, database operations include creating a database, querying a database, modifying a database, and deleting a database. The corresponding XML file is used to create an XML file, query the value of a specified node of the XML file, modify the value of a node in the XML file, and delete the value of a node in the XML file.
First, let's take a look at the XML file formats. The following lists some frequently used XML files:
Example1.xml: <? XML version = "1.0"?> <Hello> world </Hello> example2.xml: <? XML version = "1.0"?> <Poetry> <verse> alas great world Alas (again) </verse> </poetry> example3.xml: <? XML version = "1.0"?> <Shapes> <circle name = "int-based" x = "20" Y = "30" r = "50"/> <point name = "float-based" x = "3.5" Y = "52.1"/> </shapes> example4.xml: <? XML version = "1.0"?> <MyApp> <messages> <welcome> welcome to MyApp </welcome> <farewell> Thank you for using MyApp </farewell> </messages> <Windows> <window name =" mainframe "x =" 5 "Y =" 15 "W =" 400 "H =" 250 "/> </Windows> <connection IP =" 192.168.0.1 "timeout =" 123.456000" /> </MyApp>
The above example is taken from tinyxml tutorial Chinese guide. There are four examples above. How many forms of XML files do you see? I see that the attribute value is essentially in two forms: the attribute value is in the angle brackets, for example, if <window name = "mainframe" x = "5" Y = "15" W = "400" H = "250"/> and the text is out of angle brackets, for example, <welcome> welcome to MyApp </welcome>:
In view of the complexity of example4.xml, the following example describes the use of tinyxml.
Tinyxml uses two compilation options: Use the char * type of standard C or use STD: String in STL to use the pre-processor tixml_use_stl for control, that is, add? Tixml_use_stl indicates that STD: string is used. In view of the wide use of STL and its powerful functions, I will use the tinyxml description of STD: String below.
First, use VS 2005 to open the tinyxmlstl. DSP project file, compile it into a static library, debug version: tinyxmld_stl.lib, and then start testing the tinyxml library. My testing plan is like this: first create example4.xml using the tinyxml library, read it, and then query the attributes or text of the specified node, modify example4.xml (modify some node values and delete one of the nodes, add? A node), and then read it out to determine whether the change was successful. In details, create a new console project: Test in VS 2005. Be sure to use the multi-Byte Character Set for compilation. Add it at the same time ?. First, the code for creating an XML file:
/*! */Brief: Create an XML file. **/Param xmlfile: full path of the XML file. */Return: whether the request is successful. True indicates success, and false indicates failure. */Bool createxml (STD: String xmlfile) {// defines a tixmldocument class pointer tixmldocument * pdoc = new tixmldocument; If (null = pdoc) {return false ;} tixmldeclaration * pdeclaration = new tixmldeclaration (_ T ("1.0"), _ T (""), _ T (""); If (null = pdeclaration) {return false;} pdoc-> linkendchild (pdeclaration); // generate a root node: myapptixmlelement * prootele = new tixmlelement (_ T ("MyApp ")); if (null = prootele) {return false;} pdoc-> linkendchild (prootele); // generate a subnode: messagestixmlelement * PMSG = new tixmlelement (_ T ("messages"); If (null = PMSG) {return false;} prootele-> linkendchild (PMSG ); // generate a subnode: welcometixmlelement * pwelcome = new tixmlelement (_ T ("welcome"); If (null = pwelcome) {return false ;} PMSG-> linkendchild (pwelcome); // set the value of the welcome node STD: String strvalue = _ T ("Welcome to MyApp "); tixmltext * pwelcomevalue = new tixmltext (strvalue); pwelcome-> linkendchild (pwelcomevalue); // generates a subnode: farewelltixmlelement * pfarewell = new tixmlelement (_ T ("farewell ")); if (null = pfarewell) {return false;} PMSG-> linkendchild (pfarewell ); // set the value of the farewell node strvalue = _ T ("Thank you for using MyApp"); tixmltext * pfarewellvalue = new tixmltext (strvalue); pfarewell-> linkendchild (pfarewellvalue ); // generate a subnode: windowstixmlelement * pwindows = new tixmlelement (_ T ("Windows"); If (null = pwindows) {return false ;} prootele-> linkendchild (pwindows); // generates a subnode: windowtixmlelement * pwindow = new tixmlelement (_ T ("window"); If (null = pwindow) {return false;} pwindows-> linkendchild (pwindow); // set the value of the node window pwindow-> setattribute (_ T ("name "), _ T ("mainframe"); pwindow-> setattribute (_ T ("X"), _ T ("5 ")); pwindow-> setattribute (_ T ("Y"), _ T ("15"); pwindow-> setattribute (_ T ("W "), _ T ("400"); pwindow-> setattribute (_ T ("H"), _ T ("250"); // generate a subnode: windowtixmlelement * pconnection = new tixmlelement (_ T ("connection"); If (null = pconnection) {return false;} prootele-> linkendchild (pconnection ); // set the value of node connection pconnection-> setattribute (_ T ("ip"), _ T ("192.168.0.1 ")); pconnection-> setattribute (_ T ("timeout"), _ T ("123.456000"); pdoc-> SaveFile (xmlfile); Return true ;}
I wonder if you noticed the rule above? First, the parent node connects to the byte point and uses the linkendchild function. Usage: pparentnode-> linkendchild (pchild ); second, set a structure similar to this <window name = "mainframe" x = "5" Y = "15" W = "400" H = "250"/> using the setattribute function for callback, this function has two keys. The first one represents the key, and the last one represents the key value, set a structure like <farewell> Thank you for using MyApp </farewell> to use the tixmltext class and use the linkendchild function for link.
The above is the code for creating an XML file. The following describes the code for reading an XML file. The code for printing the entire XML file is very easy. The Code is as follows:
/*! */Brief prints the XML file. **/Param xmlfile: full path of the XML file. */Return: whether the request is successful. True indicates success, and false indicates failure. */Bool paintxml (STD: String xmlfile) {// defines a tixmldocument class pointer tixmldocument * pdoc = new tixmldocument (); If (null = pdoc) {return false ;} pdoc-> LoadFile (xmlfile); pdoc-> Print (); Return true ;}
Next we will introduce how to use the tinyxml library to query specified nodes, delete specified nodes, modify and add specified nodes? Node usage.
Exam documents:
1. tinyxml tutorial
2. tinyxml notes and summary
3. tinyxml tutorial Chinese Guide