XML parser for C + +: TinyXML

Source: Internet
Author: User
Tags xml parser

TinyXMLis an open source parsingXMLThe Analytic library that can be used toC + +, to be able toWindowsorLinuxcompiled in. The model of this analytic library is parsed byXMLfile, and then live in memoryintoDOMmodel, so that we can easily traverse the treeXMLTree.

The following is the inheritance relationship for the library's classes:


In TinyXML, some classes are defined according to the various elements of XML:

Tixmlbase: The base class for the entire TinyXML model.

Tixmlattribute: A property that corresponds to an element in XML.

Tixmlnode: Corresponds to a node in the DOM structure.

Tixmlcomment: corresponding to comments in XML

Tixmldeclaration: Corresponds to the declaration part of XML, namely < Versiong= "1.0"?>.

Tixmldocument: The entire document that corresponds to XML.

Tixmlelement: The element that corresponds to the XML.

Tixmltext: The text portion corresponding to the XML

Tixmlunknown: Corresponds to an unknown part of XML.

Tixmlhandler: Defines some operations for XML.

TinyXML uses two compilation options: the char * type of standard C, or the use of std::string in STL, which uses the preprocessor Tixml_use_stl for control, which adds tixml_use_stl to use std::string. Given the extensive use of STL and its powerful features, I'll use std::string's tinyxml instructions below.

Here is a specific code example:

<span style= "FONT-SIZE:18PX;" > #include <string> #include <iostream> #include <exception> #include "tinyxml.h" #include " Tinystr.h "using namespace std; #include <windows.h> #include <atlstr.h>cstring getapppath () {TCHAR Modulepath[max_path]; GetModuleFileName (Null,modulepath,max_path); CString Strmodulepath (Modulepath); Strmodulepath=strmodulepath.left (strmodulepath.reversefind (' \ \ ')); return Strmodulepath;} BOOL Createxmlfile (const string& fullPath) {try{//creates an XML Document object. Tixmldocument *mydocument = new Tixmldocument (); if (!mydocument) {return false;} Define an XML file header declaration tixmldeclaration *pdeclartion=new tixmldeclaration ("1.0", "UTF-8", "yes"); if (!pdeclartion) {retur n false;} Mydocument->linkendchild (pdeclartion);//Create a root element and connect it. Tixmlelement *rootelement = new Tixmlelement ("Persons"), if (!rootelement) {return false;} Mydocument->linkendchild (rootelement);//Create a person element and connect tixmlelement *personelement=new tixmlelement ("person") ; if (!personelement) {return false;} ROotelement->linkendchild (personelement);//Set the property of the person element Personelement->setattribute ("ID", "1");//Create the name element, The age element and connects Tixmlelement *nameelement=new tixmlelement ("name"); if (!nameelement) {return false;} Tixmlelement *ageelement=new tixmlelement ("age"); if (!ageelement) {return false;} Personelement->linkendchild (nameelement);p ersonelement->linkendchild (ageelement);// Set the name element and the age element content Tixmltext *namecontent=new tixmltext ("I am NO1"); if (!namecontent) {return false;} Tixmltext *agecontent=new Tixmltext (""); if (!agecontent) {return false;} Nameelement->linkendchild (namecontent); Ageelement->linkendchild (agecontent); MyDocument->SaveFile ( Fullpath.c_str ());d elete MyDocument;} catch (exception& e) {cout<<e.what () <<endl;return false;} return true;} BOOL ReadXml (const string& fullPath) {try{//creates an XML Document Object Tixmldocument *mydocument=new tixmldocument (fullpath.c_ STR ()); if (!mydocument) {return false;} Mydocument->loadfile ();//get root element, i.e. Personstixmlelement *rootelement=mydocument->rOotelement ();//output root element name Cout<<rootelement->value () <<endl;//get first person node Tixmlelement *firstelement =rootelement->firstchildelement ();//Gets the name node of the first person and the age node Tixmlelement *nameelement=firstelement-> Firstchildelement (); Tixmlelement *ageelement=nameelement->nextsiblingelement (); Tixmlattribute *idattribute=firstelement->firstattribute (); Cout<<nameelement->firstchild () Value () <<endl;cout<<ageelement->firstchild ()->value () <<endl;cout<<idattribute- >value () <<endl;delete myDocument;} catch (exception& e) {cout<<e.what () <<endl;return false;} return true;} /*!*/brief prints an XML file. * */param xmlfile XML file full path. */return is successful. True succeeds, False indicates failure. */bool paintxml (std::string xmlfile) {//define a Tixmldocument class pointer tixmldocument *pdoc = new Tixmldocument (); if (NULL==PDOC) { return false;} Pdoc->loadfile (Xmlfile.c_str ());pD oc->print ();d elete Pdoc;return true;} int main () {CString apppath=getapppath (); string Filename= "xmldemo.xML "; string seperator=" \ "; string fullpath=apppath.getbuffer (0) +seperator+filename;//create createxmlfile (FullPath);// Print Xmlpaintxml (FullPath);cout<< "read information as follows:" <<endl;//read Xmlreadxml (fullPath); System ("pause"); return 0;} </span>












Specific code can be downloaded:

http://download.csdn.net/detail/woshizfs/7699843

On the issue of tinyxmldocument revocation
Wrote an XML read-write program, new and delete are used in pairs, but during debugging, the program always crashes, a memory leak problem, and later found that a pointer call is empty (this is the case in C + +).

On the other hand, it is found that many new are used to create nodes using the TinyXML process. C + + needs to manage the memory itself. The objects created by new are saved on the heap and must be deleted manually. But I did not do the processing, began to confuse.

Looking for a long time, from the perspective of inheritance, for a Tinyxmldocument object, in the destruction of the child node will be released, its child nodes are new out, also must delete. But TinyXML's destructor is empty, nothing is written, do not forget, tinyxmldocument is inherited from Tinyxmlnode, in the Tinyxmlnode destructor, but clearly written

Tixmlnode::~tixmlnode ()
{
tixmlnode* node = firstchild;
tixmlnode* temp = 0;

while (node)
{
temp = node;
node = node->next;
Delete temp;
}
}

So, when the Doc object is released, it frees all the nodes that are already connected, so there's no need to manually release the new thing.
However, it is also important to note that if the Tixmldocument object is also new, you need to perform a delete on the Tixmldocument object to trigger the destructor to be called.


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.