C ++ TinyXml operation preface TinyXML is an open-source parsing XML library that can be used for C ++, compiling in Windows or Linux, and parsing C ++ XML using TinyXML, easy to use. 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. TinyXML introduces TinyXML, which defines some classes based on 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. The running environment of this article is: Redhat 5.5 + g ++ version 4.6.1 + GNU Make 3.81 + tinyxml_2_6_2 is: after extracting the http://sourceforge.net/projects/tinyxml/, get the folder tinyxml, tinyxml header file and cpp file, are in this folder, to manage our project, we still need to organize tinyxml. Tinyxml not only supports Linux compilation, but also windows compilation. After decompression, there are not only H files, cpp files, but also some project files of the vc project, here we only use it on Linux, so only the H file and cpp file are left. Other files are deleted. Here I will list the working directories after sorting: tinyxml // working directory | -- include // header file root directory | -- tinyxml // tinyxml header file, including tinystr. h tinyxml. h | -- src // cpp source code file root directory | -- tinyxml // tinyxml source code folder, including tinystr. cpp tinyxml. cpp tinyxmlerror. cpp tinyxmlparser. cpp | -- main. cpp // Our main function calls the sample code of tinyxml | -- conf // The folder where the xml file used in our example is located | -- makefile // mak For more information about efile, see the simplest example of makefile best practices in my blog. We will create student in the conf directory. the xml Code is as follows: <School name = "Software Institute"> <Class name = "C ++"> <Student name = "tinyxml" number = "123"> <email> tinyxml@163.com </email> <address> China </address> </Student> <Student name = "jsoncpp" number = "456"> <email> jsoncpp@gmail.com </email> <address> USA </address> </Student> </Class> </School> tinyxml, we only need to include <tinyxml. h>. Print the entire XML Code as follows: void printSchoolXml () {using namespace std; TiXmlDocument doc; const char * xmlFile = "conf/school. xml "; if (doc. loadFile (xmlFile) {doc. print ();} else {cout <"can not parse xml conf/school. the xml "<endl ;}} reads the following XML code: void readSchoolXml () {using namespace std; const char * xmlFile =" conf/school. xml "; TiXmlDocument doc; if (doc. loadFile (xmlFile) {doc. print ();} else {cout <<"Can not parse xml conf/school. xml "<endl; return;} TiXmlElement * rootElement = doc. rootElement (); // School element TiXmlElement * classElement = rootElement-> FirstChildElement (); // Class Element TiXmlElement * studentElement = classElement-> FirstChildElement (); // Students (; studentElement! = NULL; studentElement = studentElement-> NextSiblingElement () {TiXmlAttribute * attributeOfStudent = studentElement-> FirstAttribute (); // obtain the name attribute of student for (; attributeOfStudent! = NULL; attributeOfStudent = attributeOfStudent-> Next () {cout <attributeOfStudent-> Name () <":" <attributeOfStudent-> Value () <std :: endl;} TiXmlElement * studentContactElement = studentElement-> FirstChildElement (); // obtain the first contact information of student for (; studentContactElement! = NULL; studentContactElement = studentContactElement-> NextSiblingElement () {string contactType = studentContactElement-> Value (); string contactValue = studentContactElement-> GetText (); cout <contactType <":" <contactValue <std: endl ;}} writes data to xml. Here we write code through xml, and write almost the same as conf/school. xml also content to conf/school-write.xml, the Code is as follows: void writeSchoolXml () {using namespace std; const char * xmlFile = "conf/school-write.xml"; TiXmlDocument doc; tiXmlDeclaration * decl = new TiXmlDeclaration ("1.0", "", ""); TiXmlElement * schoolElement = new TiXmlElement ("School "); tiXmlElement * classElement = new TiXmlElement ("Class"); classElement-> SetAttribute ("name", "C ++"); TiXmlElement * stu1Element = new TiXmlElement ("Student "); stu1Element-> SetAttribute ("name", "tinyxml"); stu1Element-> SetAttribute ("number", "123"); TiXmlElement * stu1EmailElement = new TiXmlElement ("email "); stu1EmailElement-> LinkEndChild (new TiXmlText ("tinyxml@163.com"); TiXmlElement * stu1AddressElement = new TiXmlElement ("address"); stu1AddressElement-> LinkEndChild (new TiXmlText ("China ")); stu1Element-> LinkEndChild (stu1EmailElement); stu1Element-> LinkEndChild (stu1AddressElement); TiXmlElement * stu2Element = new TiXmlElement ("Student"); stu2Element-> SetAttribute ("name ", "jsoncpp"); stu2Element-> SetAttribute ("number", "456"); TiXmlElement * stu2EmailElement = new TiXmlElement ("email "); stu2EmailElement-> LinkEndChild (new TiXmlText ("jsoncpp@163.com"); TiXmlElement * stu2AddressElement = new TiXmlElement ("address"); stu2AddressElement-> LinkEndChild (new TiXmlText (" ")); stu2Element-> LinkEndChild (parent); stu2Element-> LinkEndChild (parent); classElement-> LinkEndChild (stu1Element); classElement-> LinkEndChild (stu2Element); schoolElement-> LinkEndChild (classElement ); doc. linkEndChild (decl); doc. linkEndChild (schoolElement); doc. saveFile (xmlFile );}