TinyXML Common C + + XML parser very good _c language

Source: Internet
Author: User
Tags joins object model xml parser

Reading and setting the XML configuration file is the most common operation, using several C + + XML parsers, the personal sense of tinyxml is most comfortable to use, because its API interface and Java is very similar, object-oriented very good.
TinyXML is an open source analytic library of parsing XML that can be used in C + + to compile in Windows or Linux. The model of the parsing library makes it easy to traverse the XML tree by parsing the XML file and then generating the DOM model in memory.
The DOM model, the Document object model, divides the entire document into elements (such as books, chapters, sections, paragraphs, and so on), and uses a tree structure to represent the sequential relationships between these elements and the nested inclusion relationships.
The following is an XML fragment:

Copy Code code as follows:

<Persons>
<person id= "1" >
<name> Week star </name>
<age>20</age>
</Person>
<person id= "2" >
<name> Bai Jingjing </name>
<age>18</age>
</Person>
</Persons>

In TinyXML, some classes are defined according to the various elements of XML:
Tixmlbase: The base class for the entire TinyXML model.
Tixmlattribute: attributes that correspond to elements in the XML.
Tixmlnode: Corresponds to nodes in the DOM structure.
Tixmlcomment: corresponding to annotations in XML
Tixmldeclaration: Corresponds to the Declaration section in XML, ie.? Versiong= "1.0"?>.
Tixmldocument: The entire document that corresponds to XML.
Tixmlelement: An element that corresponds to XML.
Tixmltext: Corresponding to the text part of XML
Tixmlunknown: Corresponds to an unknown part of XML.
Tixmlhandler: Defines some of the actions for XML.
TinyXML is a parsing library, mainly composed of DOM model classes (Tixmlbase, Tixmlnode, Tixmlattribute, Tixmlcomment, Tixmldeclaration, Tixmlelement, Tixmltext , Tixmlunknown) and Operation Class (Tixmlhandler) constitute. It is composed of two header files (. h files) and four CPP files (. cpp files), as long as the (Tinyxml.h, Tinystr.h, Tinystr.cpp, Tinyxml.cpp, Tinyxmlerror.cpp, Tinyxmlparser.cpp) To import the project can use its stuff. If necessary, it can be made into its own DLL to invoke. Give an example to explain everything ...
corresponding XML file:
Copy Code code as follows:

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

program code to read and write XML files:
Copy Code code as follows:

#include <iostream>
#include "tinyxml.h"
#include "Tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h>
using namespace Std;
CString Getapppath ()
{//Get 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& szFileName)
{///create an XML file, Szfilepath the path saved for the file, False if the creation succeeds returns true
Try
{
Creates a document object for XML.
Tixmldocument *mydocument = new Tixmldocument ();
Creates a root element and joins it.
Tixmlelement *rootelement = new Tixmlelement ("Persons");
Mydocument->linkendchild (rootelement);
Creates a person element and joins it.
Tixmlelement *personelement = new Tixmlelement ("person");
Rootelement->linkendchild (personelement);
Sets the properties of the person element.
Personelement->setattribute ("ID", "1");
Creates a name element, an age element, and joins.
Tixmlelement *nameelement = new Tixmlelement ("name");
Tixmlelement *ageelement = new Tixmlelement ("Age");
Personelement->linkendchild (nameelement);
Personelement->linkendchild (ageelement);
Sets the contents of the name element and the age element and joins it.
Tixmltext *namecontent = new Tixmltext ("Week Star");
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 Readxmlfile (string& szFileName)
{//Read XML file, and traverse
Try
{
CString AppPath = Getapppath ();
string seperator = "\";
String fullpath = Apppath.getbuffer (0) +seperator+szfilename;
Creates a document object for XML.
Tixmldocument *mydocument = new Tixmldocument (FULLPATH.C_STR ());
Mydocument->loadfile ();
Gets the root element, that is, the persons.
Tixmlelement *rootelement = Mydocument->rootelement ();
Outputs the root element name, which is the output persons.
cout << rootelement->value () << Endl;
Gets the first person node.
Tixmlelement *firstperson = Rootelement->firstchildelement ();
Gets the name node and age node and id attribute of the first person.
Tixmlelement *nameelement = Firstperson->firstchildelement ();
Tixmlelement *ageelement = Nameelement->nextsiblingelement ();
Tixmlattribute *idattribute = Firstperson->firstattribute ();
Outputs the name of the first person, the week star, the age content, that is, the 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);
}

Related Article

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.