Tinyxml read/write xml Sample code

Source: Internet
Author: User
Tags dota

TinyXML is an open-source parsing XML library that can be used in C ++ and compiled in Windows or Linux. The parsing library's model parses the XML file and then generates the DOM model in the memory, so that we can easily traverse this XML tree.

The DOM model is a Document Object Model. It divides a document into multiple elements (such as books, chapters, sections, and segments ), the tree structure is used to represent the ordered relationship between these elements and the nested inclusion relationship.

The following is an XML snippet:

650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> <Persons>
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> <Person ID =" 1 ">
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> <name> Zhou Xing </name>
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> <age> 20 </age>
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> </Person>
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> <Person ID =" 2 ">
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> <name> Bai Jingjing </name>
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> <age> 18 </age>
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> </Person>
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/> </Persons>
650) this. width = 650; "src =" http://www.cnblogs.com/Images/OutliningIndicators/None.gif "style =" border: 1px solid rgb (221,221,221); height: auto; margin: 10px 0px; padding: 3px; background: rgb (236,238,241) none repeat scroll 0px 0px; "/>

In TinyXML, some classes are defined based on various elements of XML:

TiXmlBase: the base class of the entire TinyXML model.

TiXmlAttribute: attribute corresponding to the element in XML.

TiXmlNode: corresponds to a node in the DOM structure.

TiXmlComment: corresponding to the comment in XML

TiXmlDeclaration: corresponds to the declarative part in XML, that is, <? Versiong = "1.0"?>.

TiXmlDocument: corresponds to the entire XML document.

TiXmlElement: elements corresponding to XML.

TiXmlText: text part corresponding to XML

TiXmlUnknown: corresponds to the unknown part of XML.

TiXmlHandler: defines some XML operations.

TinyXML is a parsing library mainly composed of DOM model classes (TiXmlBase, TiXmlNode, TiXmlAttribute, TiXmlComment, TiXmlDeclaration, TiXmlElement, TiXmlText, TiXmlUnknown) and Operation class (TiXmlHandler.


The tinyxml component is very convenient for reading and writing XML, which is easier than boost.


The following is a template file for xml reading and writing.


<? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> <Savemaplist> <map mapid = "1" name = "Dota v8.w3x" filename = "Dota_V7.8.w3x" md5 = "Encrypt"/> <map mapid = "2" name = "Dota v9.w3x "filename =" Dota_V1.8.w3x "md5 =" yellow "/> <map mapid =" 3 "name =" Dota v11.w3x "filename =" Dota_V2.8.w3x "md5 =" yellow "/> </savemaplist>


Read/Write code


VoidwriteMapXmlData () {// save when quitTiXmlDocument xmlDoc; TiXmlDeclaration Declaration ("1.0", "UTF-8", "yes"); xmlDoc. insertEndChild (Declaration); TiXmlNode * pNode = NULL; TiXmlElement * pRootElm = new TiXmlElement ("savemaplist"); pNode = xmlDoc. insertEndChild (* pRootElm); pRootElm = pNode-> ToElement (); TiXmlElement * pChildElm = new TiXmlElement ("map"); for (int I = 0; I <m_rpgMapData _. size (); ++ I) {std: string u8node; pChildElm-> SetAttribute ("mapid", m_rpgMapData _. at (I ). mapid); common: Base_W2U8 (m_rpgMapData _. at (I ). showmapname, u8node); pChildElm-> SetAttribute ("name", u8node. c_str (); common: Base_W2U8 (m_rpgMapData _. at (I ). savename, u8node); pChildElm-> SetAttribute ("filename", u8node. c_str (); pChildElm-> SetAttribute ("md5", m_rpgMapData _. at (I ). md5.c _ str (); pNode = pRootElm-> InsertEndChild (* pChildElm);} xmlDoc. saveFile (L "config \ rpgmapd. xml ");}
VoidinitReadRpgMapXmlData () {m_rpgMapData _. clear (); std: vector <rpgMapData> (). swap (m_rpgMapData _); // first, read xml fileTiXmlDocument xmlStr; if (FALSE = xmlStr. loadFile (_ TEXT ("config \ rpgmapd. xml "), TIXML_ENCODING_UTF8) {QB_error (" announcement message format error "); return ;}// m_vecItem.clear (); TiXmlElement * root = xmlStr. rootElement (); // TeamNews rootif (root) {TiXmlElement * firstNews = root-> FirstChildElement (); while (firstNews) {rpgMapData item; std: string filename; filename = firstNews-> Attribute ("mapid"); item. mapid = atoi (filename. c_str (); filename = firstNews-> Attribute ("name"); common: Base_U82W (filename, item. showmapname); filename = firstNews-> Attribute ("filename"); common: Base_U82W (filename, item. savename); item. md5 = firstNews-> Attribute ("md5"); item. haddownload = false; m_rpgMapData _. push_back (item); firstNews = firstNews-> NextSiblingElement ();}}}


Some required types


Struct rpgMapData {int mapid; std: string md5; std: wstring showmapname; // display name std: wstringsavename; // storage name bool haddownload; rpgMapData () {mapid = 0; md5 = ""; showmapname = L ""; haddownload = false;} rpgMapData (int v): mapid (v) {md5 = ""; showmapname = L ""; haddownload = false;} bool operator = (const rpgMapData & _ R) const {return mapid = _ R. mapid ;}};


Tinyxml read/write xml Sample code

Tinyxml read/write xml Sample code

Tinyxml read/write xml Sample code

Tinyxml read/write xml Sample code

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.