Analysis and traversal of tinyxml

Source: Internet
Author: User
I learned XML text parsing and found a useful open-source program, tinyxml. Here I will share the principles of tinyxml and XML file traversal operations. Tinyxml is a simple, lightweight, XML file parser implemented by C ++. It can be easily integrated with other programs for tinyxml analysis:

The following is a document named example. XML, which will be used as an example for subsequent analysis:

Example. xml

(1) tinyxml creates an XML document into a dom (Document Object
Model) tree. The specific implementation is the firstchild-nextsibling tree. Below is a brief introduction to the tree model:

Firstchild-nextsibling is a common implementation method for multi-tree trees. Each node only needs to know its first child node (first child
Node) and its next sibling node (next sibling
Node), so that the structure of the entire tree will be established, you can also use the root node pointer as the starting point to traverse the entire tree. In tinyxml, each node stores its first child,
Last child, next sibling, previous sibling,
The pointer of the five nodes related to parent, which provides a more convenient traversal interface. The following is the DOM tree created for the preceding example. XML content:

DOM tree

Blue Points to first child, red points to last child, green points to next sibling, purple points to previud
Sibling, black pointing to parent

(2) tinyxml abstracts the elements in an XML document into objects as shown in:

Tinyxml Object Model
  • Tixmlbase: The public base class of all objects in tinyxml, which implements some common operations, such as character encoding conversion, and also defines some public data structures, such as error types.
  • Tixmlnode: The base type of the node element in the DOM tree. It defines some feature data and related operations of the DOM tree node.
  • Tixmldocument: corresponds to an object in the XML document. The root node of a Dom is of the tixmldocument type, and its node cannot be of the tixmldocument type. (Example. XML)
  • Tixmldeclaration: corresponds to the object in the declaration part at the beginning of the XML document. It mainly contains version, encode,
    Standalone data information and related operations. (<? XML version = "1.0" standalone = NO>)
  • Tixmlcomment: corresponds to the objects in the annotation section of the XML document. It mainly contains the content of the annotation and related operations. (<! -Our to do list
    Data->)
  • Tixmlelement: corresponds to the objects of common elements in XML documents. Each element has a corresponding name and can have some attributes. Tixmlelement contains the relevant information and its operations.
    (<Todo> <item priority = "1"> <bold> <item
    Priority = "2">)
  • Tixmltext: corresponds to the object of text information in the element in the XML document. It implements operations related to text information. (Go To The, toy store !, Do
    Bills)
  • Tixmlattributeset: a set of all attributes of an element in an XML document. It is a part of tixmlelement and is used to manage all attributes of an element.
  • Tixmlattribute: the object corresponding to the attribute of the element in the XML document. It is a name-Value Pair object, name is the property name, and value is the property value.
  • Tixmlunknown: all objects corresponding to content not represented by the above object.

The example. XML document and the objects defined above can have the following correspondence:

Example

 

XML file Traversal
After learning how XML files are traversed, We can parse the information we need. The parsing code is as follows:

# Include <iostream>

# Include <string>

# Include "tinyxml. H"

Using namespace STD;

Void paraseupdatexml (tixmlnode * pparent );

Int main ()

{

Tixmldocument DOC ("ABC. xml ");

Doc. LoadFile ();

Tixmlelement * root = Doc. rootelement ();

If (! Root) return 1;

Paraseupdatexml (Root );

Return 0;

}

Void paraseupdatexml (tixmlnode * pparent)

{

If (pparent = NULL)

Return;

Tixmlnode * pchild = pparent-> firstchild ();

While (pchild)

{

Cout <pchild-> value () <"";

Int T = pchild-> type ();

If (t = tixmlnode: element)

{

Tixmlattribute * ATTR = pchild-> toelement ()-> firstattribute ();

If (ATTR)

{

Tixmlnode * node = pchild;

While (node)

{

While (ATTR)

{

Cout <"" <ATTR-> name () <"=" <
ATTR-> value ();

ATTR = ATTR-> next ();

}

Cout <Endl;

Node = node-> nextsiblingelement ();

}

}

Paraseupdatexml (pchild );

}

Else if (t = tixmlnode: text)

{

Cout <pchild-> value () <Endl;

}

Pchild = pchild-> nextsibling ();

}

}

 

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.