Use of C + + read Xml,tinyxml

Source: Internet
Author: User
Tags tagname xml parser

Objective:

In the recent development process, there is a need to format the XML to convert from one format to another. Therefore, it is necessary to read the XML for processing. Originally intended to be written as a tool to run under Linux, but later considering the integration with the system, Finally, the use of the front desk JS turn. It's all about reading XML anyway, what technology doesn't turn out to be the same?

However, at the beginning of the technology to be used to do some research. C + + There are many ways to read XML. The comparison name is:

Rapidxml (This is an online introduction, no use)

xerces-c++ XML Parser: In general, the method of reading XML is to read the entire text, then build into the DOM tree, and then traverse the operation. This Parser in addition to supporting the way to build DOM tree, Processing is also supported in a manner similar to a callback function (SAX,SAX2). The corresponding node is read and the function is called for processing. The advantages of DOM tree are simple and easy to operate, but the disadvantage is obvious: the whole XML needs to be read into before it can be processed. If the XML is large, the memory will not be able to support it. The role of SAX,SAX2 is here. It can support very large XML, because it is equivalent to the event-based processing method , you do not need to build the DOM Tree. But it's more trouble.

TinyXML: This is the next library to be introduced. Named Tiny, it is intended to write a lightweight tool for processing basic XML. Therefore, the features it supports are limited. The features that are not supported are listed below:

TinyXML doesn ' t parse or use DTDs (Document Type definitions) or XSLs (extensible Stylesheet Language.)

-------------------Official documentation

The reasons given are also clear:

1. Supporting these two features makes the library more large

2. Use more complex

3. Learning curve more twists and turns

These attributes are required to consider the above xerces-c++ XML Parser. Very powerful. Of course, it's much more complicated.

TinyXML using:

Before the speech is enough, we will not talk nonsense, directly explain the use of TinyXML method.

Installation:

The installation method of the tinyxml is not complicated, or it is not installed.

1. Download TinyXML, download tinyxml_2_6_2.zip

2. Unzip the unzip Tinyxml_2_6_2.zip. (Windows will simply unzip it.)

    

For friends with VS, open Tinyxml.sln directly. For Linux users, you need to copy all 6 files above.

PS. Incidentally, I think it is possible to copy the makefile of it. Very good writing. You can refer to the reference.

Use:

The use of TinyXML is very simple, just need to put the copied files in their own directory under the writing, reference tinyxml.h,tinystr.h can be.

Simple Sample code:

    

1 #include"Tinyxml.h"2 #include"Tinystr.h"3 #include <iostream>45UsingNamespaceStd6int main (int argc,char *Argv[])7{8if (argc! =2)9{Ten cout <<"Usage"<<argv[0] <<"XMLFile"<<Endl11Return1;12}Tixmldocument Doc (argv[1]);14BOOL Loadok =Doc. LoadFile ();15if (!Loadok)16{cout <<"Could load:"<< Doc. Errordesc () <<Endl18}Tixmlprinter printer;//Provides a tool class to output XML data in a formatDoc. Accept (&printer);cout << printer. CSTR () << Endl;//Output22Tixmlelement*node = doc. Firstchildelement ();//Gets the first element nodecout << node->value () << Endl;//The value of the output node25StringTNode->queryvalueattribute ("Type", &t);//Get Node Propertiescout << T <<Endl28Doc. FirstChild ()->nextsibling ()->toelement ()->querystringattribute ("Type", &t);//Gets the data for the second child nodecout <<"2:"<< T <<Endl3132//Use traversal to processtixmlnode* Child =NULL;tixmlelement* element =NULL;Tixmlattribute *attr =NULL;36IntCt37while (child = Doc. FirstChild ()Iteratechildren (Child))38{cout << child->valuestr () <<"\ t";CT = child->Type ();cout << CT <<"\ t";42//Processing according to the different node types43Switch(CT)44{45CaseTixmlnode::tinyxml_text:46Break;47CaseTixmlnode::tinyxml_element:element = Child->Toelement ();attr = element->FirstAttribute ();50While(attr)51{cout << attr->nametstr () <<"= ' 

Compile command:

g++-C Tinyxmlparser. CPP-dtixml_use_stlg++-C tinyxmlerror.  CPP-dtixml_use_stlg++-C tinystr.  CPP-dtixml_use_stlg++-C tinyxml.  CPP-Dtixml_use_stl g++-C xmltest.  CPP-          

Or you can refer to that makefile to change.

XML file:

<XML>    <type= ' 1 '<type= ' 2 '/></xml  >           

Operation Result:

<xml>    <tag1 type="1"/>    <tag2 type="2"/></xml>XML  11 type=1 type=           

It's a bit confusing not to hit the carriage return.

Detailed Explanation:

This part is only my understanding, may be wrong. Please let me know if you have any mistakes.

TinyXML all nodes are distinguished according to XML. We can also see that the class library has:

Tixmldocument: The root in the document

Tixmlnode:node is the parent type of all the nodes in the document. It can be converted to other node types.

Tixmlelement:element node. That is, the node that we normally use, with attributes, tagname.

Tixmlcomment: Notes

Tixmltext: Text node.

Tixmldeclaration:xml declaration (? xml version= "1.0" standalone= "yes"?>)

Tixmlunknown: Any tinyxml that are not recognized will be attributed to unknown, and when the file is re-written back, it is output as-is.

In general our operation is simply to take tagname, get the attributes, get the text, iterate. Here's how to do this.

TagName: For element, you can use Element->value () or element->valuestr ();//The difference is only in the return value, which returns a string.

For the value () function, the document also explains that the return value is different for different nodes.

Document:filename of the XML fileelement:name of the elementcomment:the comment textunknown:the tag contentstext:the text String

Get Properties: There are several ways to get properties. The simplest is to use the const char* attrubute (const char *) or Queryvalueattribute (const std::string &name, T * Outvalue) Const. There is also a function to obtain the appropriate type, such as int,double. You can view the manual yourself. Sometimes you can write this when you need to traverse properties.

Tixmlattribute *attr = null;attr = element->firstattribute ();  while (attr) {      "=" << attr->valuestr (); attr = attr->Next ();}        

Get text: Element->gettext ();

Iteration:

Tixmlnode *child = NULL;  while (child = parent->Iteratechildren (child)) {    cout << child->valuestr () << Endl;} 

Summarize:

The introduction of TinyXML to this. There are a lot of details still not described, but you can look at the document, under the source of the package there is doc, very convenient, speaking is also very detailed.

Any questions can be discussed at any time. Welcome message ha.

Use of C + + read Xml,tinyxml

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.