TinyXML: an excellent C ++ XML parser, tinyxmlxml

Source: Internet
Author: User

TinyXML: an excellent C ++ XML parser, tinyxmlxml

// Certificate //-----------------------------------------------------------------------------------------------------------------------------------------------

Download the/C ++ // XML/TinyXml.rar file from my online storage. Add to the project directory. Then, add the corresponding header file and source file in Solution Explorer. This step must be done.

In this way, TinyXml is officially added to your project.

Then reference it as needed:

# Include "tinyxml. h"

# Include "tinystr. h"

Both must be referenced.

Then you can use the following software.

In actual use, only six source files are used. Their names are as follows:

<Persons> <Person ID = "1"> <name> Zhou Xingxing </name> <age> 20 </age> </Person> <Person ID = "2"> <name> Bai Jingjing </name> <age> 18 </age> </Person> </Persons>


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: corresponding 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: text part corresponding to XML

TiXmlUnknown: corresponds to the unknown part of XML.

TiXmlHandler: defines some XML operations.

TinyXML is a parsing library mainly composed of DOM model classes (TiXmlBase, TiXmlNode, TiXmlAttribute, TiXmlComment, TiXmlDeclaration, TiXmlElement, TiXmlText, TiXmlUnknown) and Operation class (TiXmlHandler. It consists of two header files (. h file) and four CPP files (. cpp file), when used, as long as (tinyxml. h, tinystr. h, tinystr. cpp, tinyxml. cpp, tinyxmlerror. cpp, tinyxmlparser. cpp) import the project to use it. If needed, you can call it as your own DLL. For example, we can illustrate everything...
Corresponding XML file:

<Persons>    <Person ID="1">        <name>phinecos</name>        <age>22</age>    </Person></Persons>

 
Program code for reading and writing XML files:

 

# Include <iostream> # include "tinyxml. h "# include" tinystr. h "# include <string> # include <windows. h> # include <atlstr. h> using namespace std; CString GetAppPath () {// obtain the application root directory TCHAR modulePath [MAX_PATH]; GetModuleFileName (NULL, modulePath, MAX_PATH); CString strModulePath (modulePath ); strModulePath = strModulePath. left (strModulePath. reverseFind (_ T ('\'); return strModulePath;} bool CreateXmlFile (string & s ZFileName) {// create an xml file. szFilePath is the path to save the file. If the file is created successfully, true is returned. Otherwise, false try {// create an XML document object. TiXmlDocument * myDocument = new TiXmlDocument ();
// Add an xml header.

TiXmlDeclaration * pDeclaration = new TiXmlDeclaration ("1.0", "UTF-8 ","");
MyDocument-> LinkEndChild (pDeclaration );

// 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); // you can specify 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 );

// This is to add an attribute
NameElement-> SetAttribute ("data", "192.168.1.12 ");

// Set and connect the content of the name and age elements. TiXmlText * NameContent = new TiXmlText (""); TiXmlText * AgeContent = new TiXmlText ("22"); NameElement-> LinkEndChild (NameContent); AgeElement-> LinkEndChild (AgeContent ); CString appPath = GetAppPath (); string seperator = "\"; string fullPath = appPath. getBuffer (0) + seperator + szFileName; myDocument-> SaveFile (fullPath. c_str (); // save to file} catch (string & e) {return false;} return true;} bool ReadXmlF Ile (string & szFileName) {// read the Xml file and traverse try {CString appPath = GetAppPath (); string seperator = "\"; string fullPath = appPath. getBuffer (0) + seperator + szFileName; // create an XML document object. TiXmlDocument * myDocument = new TiXmlDocument (fullPath. c_str (); myDocument-> LoadFile (); // obtain the root element, namely, Persons. TiXmlElement * RootElement = myDocument-> RootElement (); // output the root element name, namely, 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 * idattriement = FirstPerson-> firstattriement (); // output the name of the first Person, that is, Zhou Xing; age content, that is, ID attribute, that is. Cout <NameElement-> FirstChild ()-> Value () <endl; cout <AgeElement-> FirstChild ()-> Value () <endl; cout <IDAttribute-> Value () <endl;} catch (string & e) {return false;} return true;} int main () {string fileName = "info. xml "; CreateXmlFile (fileName); ReadXmlFile (fileName );}

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.