TinyXML of C + + XML parsing

Source: Internet
Author: User
Tags gettext object model

Recently using TinyXML for C + + XML parsing, the feeling is simple to use, very easy to start, this paper gives a simple example of XML parsing using TinyXML, many complex applications can be based on this example of the method to complete. Later articles will explain the use of xerces for C + + XML parsing examples, I hope that we communicate together.

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.

First download the TinyXML library from the Internet, the name of the folder is Tinyxpath, in the project to do the following configuration:

Add in additional include path: your Tinyxpath path/tinyxpath/include

Add in additional library path: your Tinyxpath path/tinyxpath/lib

Add in Object/library path: Tinyxpathd.lib, if the release version is used, it is tinyxpath.lib.

In addition, because I developed the project is multi-threaded, so set a multithreaded environment, so the use of tinyxml did not occur. I will tinyxml write in a separate C + + project for testing, found that if you do not set up a multithreaded environment, there will be link errors. I think the reason may be that TinyXML uses a multithreaded environment, so you need to set up a multiple-threading environment. In Project/Setup, in the C + + TAB, select Code Generation and select the Debug multithreaed DLL in the use Run-time library.

The XML file for this example is students.xml as follows:

<class name= "Computer software class" >

<Students>

<student name= "John" studentno= "13031001" sex= "male" age= ">"

<phone>88208888</phone>

<address> No. Second Taibai Road, Xi ' an </address>

</student>

<student name= "Dick" studentno= "13031002" sex= "male" age= ">"

<phone>88206666</phone>

<address> Guanghua Road, Xi ' an </address>

</student>

</Students>

</Class>

The program code XmlParseExample.cpp as follows:

#include <iostream>

#include <string>

#include <tinyxml.h>

Using Std::string;

int main ()

{

tixmldocument* myDocument = new Tixmldocument ();

Mydocument->loadfile ("Students.xml");

tixmlelement* rootelement = Mydocument->rootelement ();//class

tixmlelement* studentselement = Rootelement->firstchildelement ();//students

tixmlelement* studentelement = Studentselement->firstchildelement ();//students

while (studentelement) {

tixmlattribute* attributeofstudent = Studentelement->firstattribute ();//Get Student's Name property

while (attributeofstudent) {

Std::cout "Attributeofstudent->name ()" ":" "Attributeofstudent->value ()" STD::ENDL;

Attributeofstudent = Attributeofstudent->next ();

}

tixmlelement* phoneelement = Studentelement->firstchildelement ();//Get student's phone element

Std::cout "" Phone "": "" Phoneelement->gettext () "STD::ENDL;

tixmlelement* addresselement = Phoneelement->nextsiblingelement ();

Std::cout "Address" ":" "Phoneelement->gettext ()" STD::ENDL;

Studentelement = Studentelement->nextsiblingelement ();

}

return 0;

}

The results of the program operation are as follows:

Name: John

studentno:13031001

Sex: Male

Age:22

phone:88208888

address:88208888

Name: Dick

studentno:13031002

Sex: Male

Age:20

phone:88206666

address:88206666

In this example, the XML file is used to parse, it is easy to grasp, but many developers do not know how to parse the XML character stream (non-XML file), I looked at the source code provided by TinyXML, which can use the following methods to parse XML streams. For the previous example, the code is as follows:

String xmlstring =

"<class name=" Computer software class >

<Students>

<student name= "John" studentno= "13031001" sex= "male" age= ">"

<phone>88208888</phone>

<address> No. Second Taibai Road, Xi ' an </address>

</student>

<student name= "Dick" studentno= "13031002" sex= "male" age= ">"

<phone>88206666</phone>

<address> Guanghua Road, Xi ' an </address>

</student>

</Students>

</Class> ";

tixmldocument* myDocument = new Tixmldocument ();

Mydocument->parse (Xmlstring.c_str ());

Using the parse function, you can parse an XML character stream, a situation that many developers are not familiar with.

If a developer develops a particular application, you can use a similar method like this, and you may not need to fully process each attribute, such as the attribute name can be judged, only the attributes you need, or the XML elements you need. You can also use the TinyXML method to create XML elements and XML attributes, or set XML elements and attributes corresponding to the value, and so on, if the reader wants similar examples, you can write a message.

Some of the classes in TinyXML are described below. 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, 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.

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.