TinyXML2 Open Source Library uses

Source: Internet
Author: User
Tags gettext
related articles

TinyXML2 Study: http://www.tuicool.com/articles/uYBB7j

C + + uses TinyXML to create and parse XML files (this article is for COCOS2DX): http://blog.csdn.net/u012234115/article/details/38643259 Declaration

I am because of the work of the use of TinyXML, the source code is directly provided to me by the boss, and the spread of the internet is not the same, so I do not know where the download link, but I will be included in the end of the article source
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:
    xmlbase: The base class for the entire TinyXML model.
    XmlAttribute: A property that corresponds to an element in XML.
    XmlNode: Corresponds to a node in the DOM structure.
    xmlcomment: Corresponds to the comment in the XML.
    XmlDeclaration: Corresponds to the declaration part in XML, that is, <. Versiong= "1.0"?>.
    XmlDocument: The entire document that corresponds to XML.
    XmlElement: The element that corresponds to the XML.
    XmlText: Corresponds to the text portion of the XML.
    Xmlunknown: Corresponds to an unknown part of XML. 
    Xmlhandler: Defines some operations for XML.
Application Examples
Get familiar with the API by creating and reading the following XML
<?xml version= "1.0"?>  
<school name= "Software Academy" >  
    <class name = "C + +" >  
        <student name= " TinyXML "number=" 123 ">  
            <email>tinyxml@163.com</email>  
            <address> China </address>           
        </Student>  
        <student name= "Jsoncpp" number= "456" >  
            <email>jsoncpp@gmail.com</ Email>  
            <address> USA </address>           
        </Student>  
    </Class>  
Creating XML
BOOL Createxml () {XMLDocument *doc = new XMLDocument;
    XmlDeclaration *declaration = doc->newdeclaration ("xml version=\" 1.0\ "encoding=\" Gbk\ "");
    Doc->linkendchild (declaration);
    XMLElement *eleschool = doc->newelement ("School");

    Doc->linkendchild (Eleschool);    Eleschool->setattribute ("name", "Software Academy");
    Set Property XMLElement *eleclass = Doc->newelement ("Class");  Eleschool->linkendchild (Eleclass);

    Hang on the school node Eleclass->setattribute ("name", "C + +");
    ------------------------------First Student XMLElement *elestudent = doc->newelement ("Student"); Eleclass->linkendchild (elestudent);
    Hang on the school node//can set multiple properties Elestudent->setattribute ("name", "TinyXML");

    Elestudent->setattribute ("number", 123);
    XMLElement *eleemail = doc->newelement ("email");
    Elestudent->linkendchild (Eleemail);
    XMLText *texemail = Doc->newtext ("tinyxml@163.com");

    Eleemail->linkendchild (Texemail); XmlelemeNT *eleaddress = doc->newelement ("Address");
    Elestudent->linkendchild (eleaddress);
    XMLText *texaddress = Doc->newtext ("China");

    Eleaddress->linkendchild (texaddress);
    ------------------------------A second student XMLElement *elestudent1 = doc->newelement ("Student");    Eleclass->linkendchild (ELESTUDENT1);
    Hang on the school node Elestudent1->setattribute ("name", "Jsoncpp");

    Elestudent1->setattribute ("number", 456);
    XMLElement *eleemail1 = doc->newelement ("email");
    Elestudent1->linkendchild (ELEEMAIL1);
    XMLText *texemail1 = Doc->newtext ("jsoncpp@gmail.com");

    Eleemail1->linkendchild (TEXEMAIL1);
    XMLElement *eleaddress1 = doc->newelement ("Address");
    Elestudent1->linkendchild (ELEADDRESS1);
    XMLText *texaddress1 = Doc->newtext ("United States");

    Eleaddress1->linkendchild (TEXADDRESS1);
        if (xml_success! = Doc->savefile ("E:/tmp.xml")) {delete doc;
    return false; } doc->savefile ("e:/Tmp.xml ");
    Delete doc;
return true; }
Read
BOOL ReadXml () {XMLDocument doc; if (xml_success! = doc.
    LoadFile ("E:/tmp.xml"))//load XML failed to return false {return false; } XMLElement *eleschool = Doc.  RootElement ();

        Get root node if (eleschool) {cout << "School:" << eleschool->attribute ("name") << Endl;
        xmlelement* Eleclass = eleschool->firstchildelement ("Class");
            while (Eleclass) {cout << "Class:" << eleclass->attribute ("name") << Endl;
            xmlelement* elestudent = eleclass->firstchildelement ("Student"); while (elestudent) {cout << "Student:" << elestudent->attribute ("name") &L
                t;< "," << Elestudent->attribute ("number") << Endl;
                xmlelement* Eleemail = elestudent->firstchildelement ("email"); if (eleemail) {cout << "Email:" << ELEEMAIL-&GT
                GetText () << Endl;
                } xmlelement* eleaddress = elestudent->firstchildelement ("Address"); if (eleaddress) {cout << "Address:" << eleaddress->gettext () &lt
                ;< Endl;
            } elestudent = Elestudent->nextsiblingelement ("Student");
        } Eleclass = Eleclass->nextsiblingelement (); }
    }
}
XMLDocument::P arse (const char* p, size_t len)
This function can parse the XML-formatted string
firstchildelement (const char* value=0) directly: Gets the first child node that is value, and the value defaults to NULL, which returns the first child.
rootelement (): Gets the root node, equivalent to the null parameter version of firstchildelement;
const xmlattribute* FirstAttribute () const: Gets the first property value;
xmlhandle nextsiblingelement (const char* _value=0): Gets the next node.
Download Address
Link: Http://pan.baidu.com/s/1pKDiU4V Password: ea72

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.