C + + reads and writes XML files through the TinyXML class library

Source: Internet
Author: User

TinyXML is an open source parsing XML parsing library that can be used in C + + and can be compiled in Windows or Linux. 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.


Before using, you need to download the TinyXML class Library: Http://download.csdn.net/detail/tennysonsky. can also be downloaded on SourceForge: http://sourceforge.net/projects/tinyxml/.


After extracting the TinyXML , add these six files to your C + + project, respectively Tinystr.h, Tinystr.cpp, Tinyxml.h, Tinyxml.cpp, Tinyxmlerror.cpp, Tinyxmlparser.cpp.


As in this example, only main.cpp is the test code:



When writing code, you only need to include the tinyxml.h header file, but at compile time you need to add all the. cpp files .


The sample code is as follows:

#include <stdio.h> #include "tinyxml.h" #include <iostream> #include <cstring>using namespace std;/* Tixmldocument: Document class, which represents the entire XML file tixmldeclaration: Declaration class, which represents the declaration part of a file tixmlcomment: A comment class, which represents the comment portion of a file Tixmlelement: element class, It is the main part of a file, and supports nested structures, typically using this structure to classify stored information, which can contain attribute classes and text classes Tixmlattribute/tixmlattributeset: element attributes, which are generally nested within elements, Some properties used to record this element tixmltext: A text object nested inside an element *///creating an XML file int writexmlfile () {tixmldocument *writedoc = new Tixmldocument; XML document pointer//document Format declaration tixmldeclaration *decl = new Tixmldeclaration ("1.0", "UTF-8", "yes"), Writedoc->linkendchild (decl); Writes the document int n = 3;//The number of parent nodes tixmlelement *rootelement = new Tixmlelement ("Info");//root element Rootelement->setattribute ("num", n ); Attribute Writedoc->linkendchild (rootelement), for (int i=0; i<n; i++)//n parent Node {tixmlelement *stuelement = new Tixmlelement ("Stu");//stu//set Properties Stuelement->setattribute ("Class", "A"), if (2 = = i) {Stuelement->setattribute (" Class "," B ");} Stuelement->setattribute ("id", i+1); Stuelement->setattribute ("Flag", (i+1) *10); Rootelement->lInkendchild (stuelement);//parent node writing document//name tixmlelement *nameelement = new Tixmlelement ("name"); Stuelement->linkendchild (nameelement); Tixmltext *namecontent = new Tixmltext ("Mike"); Nameelement->linkendchild (namecontent);//Score Tixmlelement * Scoreelement = new Tixmlelement ("score"); Stuelement->linkendchild (scoreelement); Tixmltext *scorecontent = new Tixmltext ("n"); Scoreelement->linkendchild (scorecontent);//City Tixmlelement * Cityelement = new Tixmlelement ("City"); Stuelement->linkendchild (cityelement); Tixmltext *citycontent = new Tixmltext ("Shenzhen"); Cityelement->linkendchild (citycontent);} Writedoc->savefile ("Stu_info.xml");d elete Writedoc;return 1;} Parse XML file int readxmlfile () {tixmldocument mydoc ("Stu_info.xml");//xml Document object bool Loadok=mydoc. LoadFile ();//Load document if (!loadok) {cout<< "could not loaded the test file. Error: "<<mydoc. Errordesc () <<endl;exit (1);} Tixmlelement *rootelement=mydoc. RootElement ();//root element, infocout<< "[ROOT name]" << Rootelement->value () << "\ n"; Tixmlelement *pele=rootelement;//traverse the node for (tixmlelement *stuelement = Pele->firstchildelement ();// First child element stuelement! = NULL; Stuelement = Stuelement->nextsiblingelement ())//Next sibling element {//Stuelement->value () node name cout<< StuElement- >value () << ""; Tixmlattribute *pattr=stuelement->firstattribute ();//First property while (NULL! = pattr)//Output All properties {cout<<pattr-> Name () << ":" <<pattr->value () << "";p Attr=pattr->next ();} cout<<endl;//the value of the output child element for (Tixmlelement *sonelement=stuelement->firstchildelement (); sonElement;sonElement =sonelement->nextsiblingelement ()) {Cout<<sonelement->firstchild ()->value () <<endl;}} return 1;} int main (int argc, char *argv[]) {writexmlfile ();p rintf ("\nafter write\n"); Readxmlfile (); return 0;}

The results of the compilation run are as follows:



The resulting XML file contents are as follows:

<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"? ><info num= "3" >    <stu class= "A" id= "1" flag= " Ten ">        <name>mike</name>        <score>88</score>        <city>shenzhen</city >    </Stu>    <stu class= "A" id= "2" flag= ">        <name>mike</name>        <score" >88</score>        <city>Shenzhen</city>    </Stu>    <stu class= "B" id= "3" flag= " ">        <name>mike</name>        <score>88</score>        <city>shenzhen</ City>    </Stu></Info>

This tutorial sample code download Please click this link: Http://download.csdn.net/detail/tennysonsky.

Copyright NOTICE: This blog post, mostly I compiled, or in the network collection, reproduced please indicate the source!!

C + + reads and writes XML files through the TinyXML class library

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.