Use XML files as multi-tree scripts

Source: Internet
Author: User

Currently, many software configurations, including the configuration files of the game UI, are in XML format.

There are a lot of open-source XML libraries, such as C ++ xerces, but this library is large and simple XML parsing is enough to use the tinyxml library.

Download: http://sourceforge.net/projects/tinyxml

There are also six files in it. If you have trouble, you can directly generate a lib and directly use the header file.

 

I wrote a parser using tinyxml for the multi-tree mentioned above:

# Ifndef xmlscriptparser_h _ <br/> # define xmlscriptparser_h _ </P> <p> # include <iostream> <br/> # include <string> </P> <p> # include "tree. HPP "<br/> # include" tinyxml. H "</P> <p> namespace util {<br/> using namespace kptree; </P> <p> template <class datatype> <br/> class xmlscriptparser: public tree <datatype> <br/>{< br/> STD: String file_name; // XML Script Name <br/> STD: String m_name; // name of the Multi-tree <br/> Public: <br/> T Ypedef datatype type; <br/> xmlscriptparser (STD: string name); <br/> virtual ~ Xmlscriptparser () {}; </P> <p> bool loadxmlscript (); <br/> protected: <br/> bool recursparse (tixmlelement * ptreenode, pre_order_iterator & parent ); <br/>}; </P> <p> template <class datatype> <br/> xmlscriptparser <datatype >:: xmlscriptparser (STD: string name) <br/>: file_name (name) <br/>{}</P> <p> template <class datatype> <br/> bool xmlscriptparser <datatype> :: loadxmlscript () <br/>{< br/> tixmldocument DOC (file_name. C_str (); <br/> If (! Doc. loadFile () <br/> return false; </P> <p> tixmlhandle hdoc (& Doc); <br/> tixmlelement * pelem; <br/> tixmlhandle hroot (0); </P> <p> // block: Name <br/> pelem = hdoc. firstchildelement (). element (); <br/> // shoshould always have a valid root but handle gracefully if it does <br/> If (! Pelem) <br/> return false; <br/> m_name = pelem-> value (); // m_name = "htree" </P> <p> // save this for later <br/> hroot = tixmlhandle (pelem ); </P> <p> // block: nodes </P> <p> tixmlelement * ptreenode = hroot. firstchild ("Node "). element (); </P> <p> type node; <br/> // read the name of the current node: <br/> const char * pname = ptreenode-> attribute ("name"); <br/> If (pname) <br/> node. name = pname; <br/> // read the type of the current node: <br/> const char * ptype = ptreenode-> attribute ("type "); // "root" <br/> If (ptype) <br/> node. type = ptype; </P> <p> pre_order_iterator parent = begin (); <br/> parent = insert (parent, node ); </P> <p> // parse the XML file <br/> return recursparse (ptreenode, parent ); <br/>}</P> <p> template <class datatype> <br/> bool xmlscriptparser <datatype >:: recursparse (tixmlelement * ptreenode, pre_order_iterator & parent) <br/>{< br/> If (ptreenode = NULL) <br/> return true; </P> <p> // process block can also be placed outside the loop, which seems to be slightly different </P> <p> for (ptreenode = ptreenode-> firstchildelement (); <br/> ptreenode; <br/> ptreenode = ptreenode-> nextsiblingelement () <br/>{< br/>{// process block <br/> type node; </P> <p> // read the name of the current node: <br/> const char * pname = ptreenode-> attribute ("name "); <br/> If (pname) node. name = pname; <br/> // read the type of the current node: <br/> const char * ptype = ptreenode-> attribute ("type "); <br/> If (ptype) node. type = ptype; </P> <p> pre_order_iterator child = append_child (parent, node ); <br/> // construct a type object to pass in the append_child function </P> <p> recursparse (ptreenode, child ); <br/>}</P> <p> return true; <br/>}</P> <p> /////////////////////// //////////////////////////////////////// ///////// <br/> // 4/29/2009-ryf <br/> // XML script Parsing is implemented, it is not necessary to write ^ _ ^ </P> <p> # endif

 

Test code:

 Xmlscriptparser <joint> Parser ("tree. XML "); <br/> parser. loadxmlscript (); <br/> typedef scriptparser <joint >:: iterator myiter; <br/> for (myiter it = parser. begin (); it! = Parser. end (); ++ it) <br/>{< br/> cout <"type =" <(* it ). type <"/t"; <br/> cout <"name =" <(* it ). name <"/N"; <br/>} cout <Endl;

 

The new XML script file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! --- For the use of building a htree --> <br/> <ptree> <br/> <node type = "root" name = "1"> <br/> <Node type = "joint" name = "2"> <br/> <node type = "end" name = "3"> <br/> </node> </P> <p> <node type = "joint" name = "4"> <br/> <node type = "end" name = "5"> <br/> </node> <br/> <node type = "end" name = "6"> <br/> </node> <br/> <node type = "end" name =" 7 "> <br/> </node> <br/> <node type =" end "name =" 8 "> <br/> </node> <br/> </node> </P> <p> <node type = "end" name = "9"> <br/> </node> </P> <p> <node Type = "end" name = "10"> <br/> </node> </P> <p> <node type =" joint "name =" 11 "> <br/> <node type =" joint "name =" 12 "> <br/> <node type =" end "name =" 13 "> <br/> </node> <br/> <node type =" end "name =" 14 "> <br/> </node> <br/> <node Type = "end" name = "15"> <br/> </node> <br/> <node type = "end" name = "16"> <br/> </node> <br/> </node> </P> <p> <node type = "end" name = "17"> <br/> </node> <br/> <node type = "end" name = "18"> <br/> </node> <br/> <node type = "end" name =" 19 "> <br/> </node> <br/> </ptree>

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.