Tinyxml getting started

Source: Internet
Author: User

What is XML?

XML stands for extensible markup language. It is translated as an extensible markup language. In short, you can customize data identifiers to distinguish different types of data for data exchange, for example, HTML can be understood as a simple XML language. An XML file is usually a text file and can be encoded in any way.

 

 

It is the icon of an XML file in our system. Open it with vc2005 and you can see the following content:

 


XML is also composed of these objects. Generally, we often use the following classes:
L tixmldocument: document class, which represents the entire XML file.
L tixmldeclaration: Declaration class, which represents the declaration part of the file, as shown in.
L tixmlcomment: annotation class, which represents the annotation part of the file, as shown in.
L tixmlelement: an element class, which is the main part of a file and supports nested structures. This structure is generally used to classify storage information. It can contain attribute classes and text classes, as shown in.
N tixmlattribute/tixmlattributeset: element attribute, which is usually nested in an element and used to record some attributes of this element, as shown in.
N tixmltext: a text object embedded in an element, as shown in.

 

 

Save Document Object
Of course, you can also use the SaveFile () function to save it as follows:
Bool SaveFile (const STD: string & filename) const
In the program, you can use the following:

// Load the XML document
Tixmldocument DOC ("tutorial. xml ");
Doc. LoadFile ();
Doc. Print (); // output document
Cout <Endl;
Doc. SaveFile ("tutorial.txt ");
Use the export script to open tutorial.txt. You can see the following content.

 

Returns the first root element.
In addition, the Document Object provides a practical function to return the first root object, which allows you to easily traverse the entire document structure and find the data you need. The original function is as follows:
+ Tixmlelement * rootelement ()
We will introduce the usage of the element class in detail.

Declaration class
In the standard XML file, the Declaration is the first item of the file, such as <? XML version = "1.0" standalone = "yes"?>, The declared object has three attribute values: version, encoding, and independent file declaration.
Generally, the first line of a document is the declaration object. You can convert the first child node of the Document Object to the declaration object.

// Declare an object using tinyxml
Tixmldeclaration * Decl;
Decl = Doc. firstchild ()-> todeclaration ();
Then you can use its functions. It allows you to return the current version, encoding, and other information. The original function is as follows:
+ Const char * version () const
+ Const char * encoding () const
+ Const char * standalone () const

In the program, you can use the following:

// Declare an object using tinyxml
Tixmldeclaration * Decl;
Decl = Doc. firstchild ()-> todeclaration ();
Cout <"using tinyxml declaration object (tixmldeclaration)" <Endl;
// Output the XML content corresponding to the declared object
Decl-> Print (0, 4, & Str );
Cout <STR <Endl;
// Output the attributes of declared objects separately
Cout <"version:" <Decl-> Version () <"whether it is an opposite file:" <Decl-> standalone () <"encoding method: "<Decl-> encoding () <Endl;
Cout <Endl;

 

Annotation class
This class provides explanations for XML data and is not used in programs.

Element class
An element is a container class that has an element name and can contain other elements, text, comments, and unknown nodes. These objects are collectively referred to as the node of an element, nodes can be elements, text, comments, and unknown nodes. An element can also contain any number of attributes.
The following XML code is used to describe the functions of this class.

<Element attribute = "This A attribute (this is an attribute)" Int = "1" float = "3.14">
<Subelement1>
This a text (this is a text)
</Subelement1>
<Subelement2/>
<Subelement3/>
<Subelement4/>
</Element>

Node name
In the code of the above element, element is the name of the root element. You can set and return it through the following function.
+ Const STD: string & valuestr () const
+ Void setvalue (const STD: string & _ value)

Parent node
Subelement1, subelement2, subelement3, and subelement4 are all child elements of the element. If the pointer of the current Element Object points to subelement1, subelement2, subelement3, subelement4, you can () function to return the pointer to the element object. The declaration of the parent () function is as follows:
+ Tixmlnode * parent ()

Subnode
With the pointer of the parent node, You can traverse all the child nodes.
+ Tixmlnode * firstchild ()
+ Tixmlnode * firstchild (const STD: string & _ value)
The above two functions are used to return the pointer of the first subnode object. The function with the parameter name indicates that the first subnode named _ value is returned.

+ Tixmlnode * lastchild ()
+ Tixmlnode * lastchild (const STD: string & _ value)
The above two functions are used to return the pointer of the last Node object. The function with the parameter name indicates that the last child node named _ value is returned.

You can also use the iteratechildren () function to traverse all nodes in sequence. Their function declaration is as follows:
+ Tixmlnode * iteratechildren (const tixmlnode * Previous)
+ Tixmlnode * iteratechildren (const STD: string & _ value, const tixmlnode * Previous)
The function with the parameter name indicates that only nodes with the same name are traversed.

Edit subnode
You can insert, delete, and replace all child nodes.
+ Tixmlnode * insertendchild (const tixmlnode & addthis );
+ Tixmlnode * insertbeforechild (tixmlnode * beforethis, const tixmlnode & addthis );
+ Tixmlnode * insertafterchild (tixmlnode * afterthis, const tixmlnode & addthis );
The above three functions are used to insert nodes. The insertendchild function allows you to insert new nodes to the end. The insertbeforechild and insertafterchild functions allow you to insert nodes before and after the specified node location.

+ Tixmlnode * replaceChild (tixmlnode * replacethis, const tixmlnode & withthis );
The replaceChild function is used to replace a specified node.

+ Bool removechild (tixmlnode * removethis );
The removechild function allows you to delete a specified node.
Void clear ();
The clear function deletes all child nodes of the current node (including child nodes included in the child node), but does not modify the current node.

Peer node

<Element attribute = "This A attribute (this is an attribute)" Int = "1" float = "3.14">
<Subelement1>
This a text (this is a text)
</Subelement1>
<Subelement2/>
<Subelement3/>
<Subelement4/>
</Element>
In the preceding XML code, subelement1, subelement2, subelement3, and subelement4 all belong to the same-level nodes. We also provide related functions for Traversing these nodes at the same level.

+ Tixmlnode * previussibling ()
+ Tixmlnode * previussibling (const STD: string & _ value)
You can return the pointer of the previous node based on the current node. The function with the parameter name indicates that the previous node named _ value is returned.

Of course, you can also return the pointer of the next node based on the current node. The function with the parameter name indicates that the next node named _ value is returned.
+ Tixmlnode * nextsibling ()
+ Tixmlnode * nextsibling (const STD: string & _ value)

Traversal Element
An element is a special node that starts with '<' and is followed by the element name. The nextsiblingelement function is used to return the next sibling element, while ignoring other types of nodes. Their function declaration is as follows:
+ Tixmlelement * nextsiblingelement ()
+ Tixmlelement * nextsiblingelement (const STD: string & _ value)
The function with the parameter name indicates that the next same-level element named _ value is returned.

This class also provides related functions for you to return the first child element.
+ Tixmlelement * firstchildelement ()
+ Tixmlelement * firstchildelement (const STD: string & _ value)
The function with the parameter name indicates that the next child element named _ value is returned.

Element attributes
Attributes are generally stored in elements. They are two strings connected with "=", the attribute name on the left, and the attribute value on the right of the equal sign, it is usually represented by data types such as string, integer, and floating point. For example, Pi = 3.14.
You can use the following function to return the attribute value.
+ Const STD: string * attribute (const STD: string & name) const;
+ Const STD: string * attribute (const STD: string & name, int * I) const;
+ Const STD: string * attribute (const STD: string & name, double * D) const;
In the preceding three functions, the first function uses a string to save the returned property value. The second function converts the property value to an integer and then returns the result. The third function converts the property value to a floating point number and then returns the result. However, the second and third functions record the attribute values in the form of strings and return them as function return values.
You can also use the template function:
+ Template <typename T> int queryvalueattribute (const STD: string & name, T * outvalue) const
To return the attribute value of a feature. It automatically selects the appropriate data type based on the input parameters.

In addition, this class provides the following three functions for you to set attributes. The parameter type is similar to that of the return function.
+ Void setattribute (const STD: string & name, const STD: string & _ value );
+ Void setattribute (const STD: string & name, int _ value );
+ Void setdoubleattribute (const char * Name, double value );

Firstattribute and lastattribute allow you to return the first and last attributes. Their function declaration is as follows:
+ Tixmlattribute * firstattribute ()
+ Tixmlattribute * lastattribute ()
The removeattribute function allows you to delete attributes of a specified name. Its function declaration is as follows:
+ Void removeattribute (const STD: string & name)

Summary of element Functions
Valuestr // return element name
Setvalue // set the element name
Parent // return the parent node object

Firstchild // returns the first child node
Lastchild // returns the last subnode
Iteratechildren // return to the next subnode

Insertendchild // insert a subnode after the last subnode
Insertbeforechild // insert a subnode before the specified subnode
Insertafterchild // insert a subnode after the specified subnode
ReplaceChild // Replace the specified subnode
Removechild // delete a specified subnode
Clear // delete all child nodes

Previussibling // return the previous node in the same level
Nextsibling // return the last node in the same level

Nextsiblingelement // return the last element in the same level
Firstchildelement // returns the first child Element Node
Attribute // returns the attribute value of an element.
Queryvalueattribute // return the attribute value in the element.
Setattribute // set the attribute value in the element
Firstattribute // The first property object in the returned element.
Lastattribute // The last property object in the returned element.
Removeattribute // Delete the attribute object specified in the element

Attribute Class
The attribute is a value pair. An element can have a property value, but the name must be unique.
You can use
+ Const STD: string & nametstr () const
Returned property name

You can also use the following three functions to return attribute values:
+ Const STD: string & valuestr () const
+ Int intvalue () const;
+ Double doublevalue () const;

You can also set attribute values. Their function declaration is as follows:
+ Void setname (const STD: string & _ name)
+ Void setintvalue (INT _ value );
+ Void setdoublevalue (double _ value );
+ Void setvalue (const STD: string & _ value)
The above functions are similar to the related functions in the element class. We will not repeat them here.

In element attributes, there are usually many attributes. You can use the next function to return the pointer to the next attribute object, or you can use the previous function to obtain the pointer to the previous attribute object. Their function declaration is as follows:
+ Tixmlattribute * Next ()
+ Tixmlattribute * Previous ()

 

Tinyxml uses the Document Object Model (DOM) to parse XML files. The processing method of this model is to analyze the entire XML file at a time during analysis, the corresponding tree structure is formed in the memory, and a series of interfaces are provided to users to access and edit the tree structure. This method occupies a large amount of memory, but it can provide users with an object-oriented access interface, which is more user-friendly and convenient for users. Next we will introduce the usage of each class in sequence.

Document Type
 

A document class represents an XML document. With this class, you can save, load, and print output documents. You can load the XML document to tixmldocument in the following ways.

Create Document Object
L create an empty Document Object and load an XML document
The original functions used are as follows:
+ Tixmldocument ();
+ Bool LoadFile (const STD: string & filename)
In the program, you can use the following:

// Load the XML document
Tixmldocument DOC ();
Doc. LoadFile ("tutorial. xml ");

L 2. input the document name in the constructor and call the load function to complete parsing and loading.
The original functions used are as follows:
+ Tixmldocument (const STD: string & documentname );
+ Bool LoadFile ();
In the program, you can use the following:

// Load the XML document
Tixmldocument DOC ("tutorial. xml ");
Doc. LoadFile ();

Output Document Object
The document class provides the print () function to output the current document content on the console. The original form of this function is as follows:
+ Void print () const
In the program, you can use the following:

// Load the XML document
Tixmldocument DOC ("tutorial. xml ");
Doc. LoadFile ();
Doc. Print (); // output document
The content of tutorial. XML is as follows:

<? XML version = "1.0" standalone = "yes" encoding = "UTF-8"?>

<! -- Comment comment -->

<Element attribute = "This A attribute (this is an attribute)" Int = "1" float = "3.14">
<Subelement1>
This a text (this is a text)
</Subelement1>
<Subelement2/>
<Subelement3/>
<Subelement4/>
</Element>
In the console, you can get the following output:

Because the file uses UTF-8 encoding, and the windows console uses gb2312 encoding by default, garbled code is generated.

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.