C + + TinyXML operation (including source download)

Source: Internet
Author: User

Preface

TinyXML is an open source parsing XML parsing library, can be used in C + +, can be compiled in Windows or Linux, using TinyXML for C + + XML parsing, simple and easy to use.
The model of this analytic library parses the XML file and then generates the DOM model in memory, which makes it easy to traverse the XML tree.
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 order relationships between these elements and the nested containment relationships.

TinyXML Introduction

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: Corresponds to the comment in the 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: Corresponds to the text portion of the XML.
Tixmlunknown: Corresponds to an unknown part of XML.
Tixmlhandler: Defines some operations for XML.

Download and compile

The operating environment for this article is: Redhat 5.5 + g++version 4.6.1 + GNU make 3.81 + tinyxml_2_6_2

Yes: http://sourceforge.net/projects/tinyxml/

After decompression to get the folder Tinyxml,tinyxml header files and CPP files, both in this folder, for our management of our project projects, we still make tinyxml do a collation.

Since TinyXML not only supports Linux compilation, but also supports the compilation of Windows, so after decompression not only h files, CPP files, as well as some project VC project files, here we only use on Linux, so only left H file and CPP files, all other files deleted

Here I list the working directory after finishing:

tinyxml/           //working directory
|--include //header file root directory
| |--tinyxml //tinyxml header file, including Tinystr.h tinyxml.h
|--src //cpp source file root directory
|--tinyxml //tinyxml source folder, including Tinystr.cpp tinyxml.cpp tinyxmlerror.cpp tinyxmlparser.cpp
|--main.cpp //Our main function, call TinyXML sample code
|--conf //The same folder as the XML file in our example
|--makefile //makefile, do not let us say, do not understand please see my blog makefile Best practices

The simplest example

We created the Student.xml,xml code in the Conf directory as follows:

<school name= "Software Academy" >  
<class name = "C + +" >
<student name= "TinyXML" number= "123" >
<email>[email protected]</email>
<address> China </address>
</Student>
<student name= "Jsoncpp" number= "456" >
<email>[email protected]</email>
<address> USA </address>
</Student>
</Class>
</School>

With TinyXML, we just need to include <tinyxml.h> in the header file.

Print the entire XML code as follows:

void Printschoolxml () {
using namespace Std;
Tixmldocument Doc;
const char * xmlfile = "Conf/school.xml";
if (Doc. LoadFile (xmlfile)) {
Doc. Print ();
} else {
cout << "Can not parse XML conf/school.xml" << Endl;
}
}

Read XML

The code is as follows:

void Readschoolxml () {
using namespace Std;
const char * xmlfile = "Conf/school.xml";
Tixmldocument Doc;
if (Doc. LoadFile (xmlfile)) {
Doc. Print ();
} else {
cout << "Can not parse XML conf/school.xml" << Endl;
Return
}
tixmlelement* rootelement = doc. RootElement (); School elements
tixmlelement* classelement = Rootelement->firstchildelement (); class element
tixmlelement* studentelement = Classelement->firstchildelement (); Students
for (; Studentelement! = NULL; studentelement = Studentelement->nextsiblingelement ()) {
tixmlattribute* attributeofstudent = Studentelement->firstattribute (); Get the Name property of student
for (; Attributeofstudent! = NULL; attributeofstudent = Attributeofstudent->next ()) {
cout << attributeofstudent->name () << ":" << attributeofstudent->value () << Std::endl;
}

tixmlelement* studentcontactelement = Studentelement->firstchildelement ();//Get student's first contact
for (; Studentcontactelement! = NULL; studentcontactelement = Studentcontactelement->nextsiblingelement ()) {
String contacttype = Studentcontactelement->value ();
String contactvalue = Studentcontactelement->gettext ();
cout << contacttype << ":" << contactvalue << Std::endl;
}

}
}
Write XML

Here we will write the code through the XML operation, write almost and conf/school.xml the same content to Conf/school-write.xml, the code is as follows:

void Writeschoolxml () {
using namespace Std;
const char * xmlfile = "Conf/school-write.xml";
Tixmldocument Doc;
Tixmldeclaration * decl = new Tixmldeclaration ("1.0", "", "" ");
Tixmlelement * schoolelement = new Tixmlelement ("School");
Tixmlelement * classelement = new Tixmlelement ("Class");
Classelement->setattribute ("name", "C + +");

Tixmlelement * stu1element = new Tixmlelement ("Student");
Stu1element->setattribute ("name", "TinyXML");
Stu1element->setattribute ("number", "123");
Tixmlelement * stu1emailelement = new Tixmlelement ("email");
Stu1emailelement->linkendchild (New Tixmltext ("[email protected]");
Tixmlelement * stu1addresselement = new Tixmlelement ("address");
Stu1addresselement->linkendchild (New Tixmltext ("China"));
Stu1element->linkendchild (stu1emailelement);
Stu1element->linkendchild (stu1addresselement);

Tixmlelement * stu2element = new Tixmlelement ("Student");
Stu2element->setattribute ("name", "Jsoncpp");
Stu2element->setattribute ("number", "456");
Tixmlelement * stu2emailelement = new Tixmlelement ("email");
Stu2emailelement->linkendchild (New Tixmltext ("[email protected]");
Tixmlelement * stu2addresselement = new Tixmlelement ("address");
Stu2addresselement->linkendchild (New Tixmltext ("The United States");
Stu2element->linkendchild (stu2emailelement);
Stu2element->linkendchild (stu2addresselement);

Classelement->linkendchild (stu1element);
Classelement->linkendchild (stu2element);
Schoolelement->linkendchild (classelement);

Doc. Linkendchild (decl);
Doc. Linkendchild (schoolelement);
Doc. SaveFile (xmlfile);
}

Download Project

Click to download

Compile and run steps after download

Unzip Tinyxml.zip
CD TinyXML
Make
./main

More Operations

Please refer to http://www.grinninglizard.com/tinyxmldocs/tutorial0.html

original link: http://www.cnblogs.com/ggjucheng/archive/2012/01/03/2311426.html

C + + TinyXML operation (including source download)

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.