TinyXML Parsing XML documents __ Network programming

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

L TinyXML Introduction

TinyXML is currently a very popular based on the DOM model of the XML parser, easy-to-use and small, very suitable for storing simple data, configuration files, object serialization and other data volume is not a very large operation, especially for game development, in Nebula2,cegui and other open source projects are used.

Name

Access interface

Whether validation is supported

Note

Expat

Sax/local

I don't know

Local means it also has its own access model

LibXML2

Sax/dom

Verify

TinyXml

Dom

Whether

Xml4c

Sax/dom

Verify

But with the ICU, internationalization seems to be better.

Xerces-c

Sax/dom

Verify

XML Booster

Local

I don't know

Estimate efficiency should be high

L about DOM and sax

DOM -Document Object Model

The entire XML document is parsed and read into memory at once and saved as an object for user access.

SAX -Simple APIs for XML

The XML parsing method of Sax is based on the event callback, which produces an event every time an XML element is encountered and executes a user-supplied handler function.

L TINYXML class Structure

[Tixmlbase] the base class of all tinyxml classes that holds information about the node or attribute in the original XML.

[Tixmlattribute] XML node attribute, a key value pair

[Tixmlnode] The base class of XML node, encapsulates the method of operation and maintenance to the tree structure of XML document

[Tixmlcomment] XML annotation Node

[Tixmldeclaration] XML Declaration node

[Tixmldocument] XML document node (typically the root node of an XML document)

[Tixmlelement] XML node

[Tixmltext] XML text node

[Tixmlunknown] XML node with unknown label

[Tixmlhandle] encapsulates a pointer to a node, which is automatically presented when the pointer is queried

[Tixmlvisitor] Traversal interface, describes the processing of each node, subclass implementation

[Tixmlprinter] Printer Traversal device

L reading XML files

Tixmldocument doc ("Test.xml");

Doc.loadfile("Test.xml");

L Write XML file

Tixmldocument Doc;

......

Doc.savefile("Test.xml");

L Traverse an XML document

First, the first node is obtained by FirstChild, then the next sibling node is fetched in nextsibling, so recursively traverses all nodes.

Tixmlnode::firstchild();

Tixmlnode::NextSibling();

L construct an XML document

Inserts the specified node at the end of the current node's queue

Tixmlnode::linkendchild();

Set properties for the specified node

Tixmlelement::setattribute();

Tixmlelement::setdoubleattribute();

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 consists 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 things. If necessary, it can be made into its own DLL to invoke. Give an example to illustrate everything.

corresponding XML file:

<Persons>
<person id= "1" >

<name>phinecos</name>
<age>22</age>
</Person>
</Persons>

program code to read and write XML files:

#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 XML file, Szfilepath the path saved for file, false try {//create if creation succeeds return true
        A Document object that constructs an 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"); Create the name element, the age elementVegetarian and connected.
        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) {//reads the XML file and traverses the 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); }

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.