. Introduction: I recently worked on GBT28181 to interconnect with the national standard platform. It involves message communication between processes. The message body is in xml format. I used them as strings for parsing in previous tests, now, when I was developing it, I thought of using the xml library for parsing. Because the xml library used in the last HW project is this, I will continue to use it here. In general, it is still relatively easy to use: 2. Compile and use: Use in Windows: 1. Compile and compile the lib library directly with the VS project; 2. Copy the source code to your project. I chose the second one here. I want to check the source code and debug the program easily. 1. replace tinystr. cpp, tinyxml. cpp, tinyxmlerror. cpp, tinyxmlparser. copy cpp to your project. 2. introduce the header file tinystr. h. tinyxml. h 3. directly compile and use; 3. introduction: In TinyXML, some classes are defined 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. 4. Sample Code: Read xml:
Void readXml () {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-> FirstChildE Lement (); // Students for (; 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 ;}}}
Void writeXml () {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 ("Hefei"); stu1Element-> LinkEndChild (stu1EmailElement ); stu1Element-> LinkEndChild (stu1AddressElement); TiXmlElement * stu2Element = new TiXmlElement ("Student"); stu2Element-> SetAttribute ("name", "feilong "); stu2Element-> SetAttribute ("number", "123"); TiXmlElement * stu2EmailElement = new TiXmlElement ("email"); stu2EmailElement-> LinkEndChild (new TiXmlText ("feilongxx@gmail.com ")); tiXmlElement * stu2AddressElement = new TiXmlElement ("address"); stu2AddressElement-> LinkEndChild (new TiXmlText ("China"); stu2Element-> LinkEndChild (stu2EmailElement ); stu2Element-> LinkEndChild (stu2AddressElement); classElement-> LinkEndChild (stu1Element); classElement-> LinkEndChild (stu2Element); schoolElement-> LinkEndChild (classElement); doc. linkEndChild (decl); doc. linkEndChild (schoolElement); doc. saveFile (xmlFile);} void writeXml () {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 ("Hefei"); stu1Element-> LinkEndChild (stu1EmailElement ); stu1Element-> LinkEndChild (stu1AddressElement); TiXmlElement * stu2Element = new TiXmlElement ("Student"); stu2Element-> SetAttribute ("name", "feilong "); stu2Element-> SetAttribute ("number", "123"); TiXmlElement * stu2EmailElement = new TiXmlElement ("email"); stu2EmailElement-> LinkEndChild (new TiXmlText ("feilongxx@gmail.com ")); tiXmlElement * stu2AddressElement = new TiXmlElement ("address"); stu2AddressElement-> LinkEndChild (new TiXmlText ("China"); stu2Element-> LinkEndChild (stu2EmailElement ); stu2Element-> LinkEndChild (stu2AddressElement); classElement-> LinkEndChild (stu1Element); classElement-> LinkEndChild (stu2Element); schoolElement-> LinkEndChild (classElement); doc. linkEndChild (decl); doc. linkEndChild (schoolElement); doc. saveFile (xmlFile );}