TinyXML Source Learning-hello World

Source: Internet
Author: User

TinyXML uses the Document Object Model (DOM) to parse the XML file, which is processed as a one-time parsing of the entire XML document and forming the corresponding tree structure in memory, Provides a series of interfaces to the user to access and edit the tree structure. This method takes up large memory, but it can provide the user with an object-oriented access interface, which is more user-friendly and very convenient for users.

To learn about the XML introduction of the children's shoes, please go to: XML Beginner Basics (the XML is based on a very clear ^_^)

Want to download TinyXML source of children's shoes, please go to: http://sourceforge.net/projects/tinyxml/


Just using Timyxml to parse the XML file, we have to understand a few classes in tinyxml and their relationships, the following is a diagram of several major classes in TinyXML (referenced from the TinyXML document)


To learn more about TinyXML documents, you can view the documents in the Doc folder in the TinyXML source code.

Tixmlbase is the base class for all classes, and Tixmlnode, Tixmlattribute two classes inherit from the Tixmlbase class, where the Tixmlnode class refers to all content that is <...>...<.../> included, and the node in the XML is divided into the following aspects, which are declarations, annotations, nodes, and the text between the nodes, therefore, on the basis of tixmlnode, these kinds of tixmlcomment, tixmldeclaration, Tixmldocument, Tixmlelement, Tixmltext, Tixmlunknown, respectively, are used to indicate which part of the XML is specific. The Tixmlattribute class is different from Tixmlnode, which refers to the content inside the angle brackets, like the < ***=...>, where * * * is a property.

The following is an example of a simple XML file :

<?xml version= "1.0" encoding= "UTF-8"?>  <phonebook>  <!--one item Behalfs one contacted person.-->  <item>  <name>miaomaio</name>  <addr>shaanxi Xi ' an</addr>  <tel>13759911917</tel>  <email>[email protected]</email>  </item>  <item>  <name>gougou</name>  <addr>liaoning shenyang</addr>  <tel >15840330481</tel>  <email>[email protected]</email>  </item>  <!-- More contacted Persons.-->  </phonebook>  

L like Tixmldeclaration refers to <?xml version= "1.0" encoding= "UTF-8"?

L like Tixmlcomment refers to <!--one item Behalfs onecontacted person.-->, <!--more contacted Persons.-->

L like Tixmldocument refers to the entire XML document,

L like Tixmlelement refers to <phonebook>, <item>, <name>, <addr> and so on these nodes,

L like Tixmltext refers to ' Gougou ', ' 15840330481 ' which are sandwiched between <item> and </item>, <name> and </name>, <addr> and </addr> text between the texts,

L like Tixmlattribute refers to <?xml version= "1.0" encoding= "UTF-8"?> node in version, encoding,

L Besides that is tixmlunknown.

Note

<phonebook>......</phonebook>

This is a tixmlnode, and ... Is that the tixmlelement,tixmlelement of the Tixmlnode does not include tixmlcomment (the annotation class, which represents the comment portion of the file )


The following is a simple Hello World Program(Not output Hello World yo)
/* * Create a new XML file, it's name is Phonebook.xml * Timyxml version:2.6.2 * program creates a new empty doc file, and then adds a phonebook node to the file, at which point The root node of the doc file is phonebook, * Add 3 Contact information (name, addr, tel, email) to the root node and print out the information for each contact. * Then delete a message from one of the contacts, such as the name of the first contact, then delete a contact and print it out */#include <iostream> #include <sstream> #include " Tinyxml.h "using namespace std;/** * Insert Name, addr, tel, email and other nodes into the root node */int phonebook_insertendchild (tixmlnode* root, char* _name = "", char* _addr = "", char* _tel = "", char* _email = "") {if (!root) return-1; tixmlelement* node = new Tixmlelement ("item"); tixmlnode* name = new Tixmlelement ("name"), Node->insertendchild (*name)->insertendchild (Tixmltext (_name)); tixmlnode* addr = new Tixmlelement ("addr"), Node->insertendchild (*ADDR)->insertendchild (Tixmltext (_ADDR)); tixmlnode* Tel = new Tixmlelement ("tel"), Node->insertendchild (*tel)->insertendchild (Tixmltext (_tel)); tixmlnode* email = new Tixmlelement ("email"), Node->insertendchild (*email)->insertendchild (Tixmltext (_email));/* Add to root the element,element contains 4 nodes */root->insertendchild (*node); return 0;} int main (void) {const char* str = "<? XML version = \ "1.0\"?> "; Tixmldocument doc ("Phonebook.xml");d OC. Parse (str); tixmlnode* phonebook = new Tixmlelement ("Phonebook");d OC. Insertendchild (*phonebook); /* Insert node Phonebook *//* gets the root element of Doc, which is phonebook */tixmlnode* root = Doc. RootElement (); Phonebook_insertendchild (Root, "hdu1", "Hang Zhou", "N", "[email protected]"); Phonebook_insertendchild (Root, "hdu2", "Hang Zhou", "111", "[email protected]"); Phonebook_insertendchild (Root, "hdu3", "Hang Zhou", "email protected");d OC. Print (); cout << endl;/* iterates through item, then outputs the node (name,addr,tel,email) information in each item */for (tixmlnode* item = root-> FirstChild (); Item Item = item->nextsibling ("item") {cout << "-----------------------------" << endl;for (tixmlelement* element = Item->firstchildelement (); Element element = Element->nextsiblingelement ()) {if (!strcmp (Element->valuE (), "name")) cout << "Name:" << (Element->gettext ()? Element->gettext (): "") << Endl;else if (!strcmp (Element->value (), "addr") cout << "addr:" << (E Lement->gettext ()? Element->gettext (): "") << Endl;else if (!strcmp (Element->value (), "tel")) cout << "Tel:" << (ele Ment->gettext ()?  Element->gettext (): "") << Endl;else if (!strcmp (Element->value (), "email") cout << "Email:" << (Element->gettext ()? Element->gettext (): "") << endl;elsecout << "I don ' t know" << Endl;} Doc. SaveFile (); /* The doc is saved as a phonebook.xml file after the comment is canceled */tixmlelement* element = Root->firstchild ()->firstchildelement ();root-> FirstChild ()->removechild (element); /* Delete the <name>hdu1</name> node in the first item node */tixmlnode* nodes = Root->lastchild (); Root->removechild (node   ); /* Delete the node <item><name>hdu3</name><addr>hang zhou</addr><tel>112</tel><email>[email protected]</email></item> */doc. Print (); cout << endl;for (tixmlnode* item = Root->firstchild (); item; item = item->nextsibling ("item")) {cout << "-----------------------------" << endl;for (tixmlelement* element = Item->firstchildelement (); Element element = Element->nextsiblingelement ()) {if (!strcmp (Element->value (), "name") cout << "Name:" << ( Element->gettext ()? Element->gettext (): "") << Endl;else if (!strcmp (Element->value (), "addr") cout << "addr:" << (E Lement->gettext ()? Element->gettext (): "") << Endl;else if (!strcmp (Element->value (), "tel")) cout << "Tel:" << (ele Ment->gettext ()?  Element->gettext (): "") << Endl;else if (!strcmp (Element->value (), "email") cout << "Email:" << (Element->gettext ()? Element->gettext (): "") << endl;elsecout << "I don ' t know" << Endl;} return 0;}
When no elements are removed, the contents of the Phonebook.xml file are:

<? XML version = "1.0"?><phonebook>    <item>        <name>hdu1</name>        <addr>hang zhou</addr>        <tel>110</tel>        <email>[email protected]</email>    </item >    <item>        <name>hdu2</name>        <addr>hang zhou</addr>        <tel> 111</tel>        <email>[email protected]</email>    </item>    <item>        <name>hdu3</name>        <addr>hang zhou</addr>        <tel>112</tel>        < Email>[email protected]</email>    </item></phonebook>
Deleted the contents of an item node item and the Phonebook.xml file after the name item in the first item node

<? XML version = "1.0"?><phonebook>    <item>        <addr>hang zhou</addr>        <tel> 110</tel>        <email>[email protected]</email>    </item>    <item>        < name>hdu2</name>        <addr>hang zhou</addr>        <tel>111</tel>        <email >[email protected]</email>    </item></phonebook>

Resources:

1. Basic knowledge of getting started with XML

2. TinyXML use of C + +
3, TinyXML Source

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

TinyXML Source Learning-hello World

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.