By downloading the latest version of tinyxml in the http://www.grinninglizard.com/tinyxml/, decompress the package, we can view the usage of tinyxml in docs, In order to quickly master, you can open tixmlbase in class hierarchy to find its class graph structure.
Below is my personal code to master or test tinyxml:
// # Define tixml_use_stl <br/> # include "tinystr. H "<br/> # include" tinyxml. H "<br/> # include <string> <br/> // test <br/> # define testread <br/> // # define testwrite <br/> void main () <br/>{< br/> # ifdef testread <br/> char * Buf = "<? XML version = '1. 0' encoding = 'utf-8'?> <Soap: envelope xmlns: Soap =/"http://www.w3.org/2003/05/soap-envelope/" "<br/>" xmlns: D =/"http://schemas.xmlsoap.org/ws/2005/04/discovery/" xmlns: wsadis =/"http://schemas.xmlsoap.org/ws/2004/08/addressing/" "<br/>" xmlns: DN =/"http://www.onvif.org/ver10/network/wsdl/"> "<br/>" <soap: Header> <wsadis: messageid> UUID: "<br/>" </wsadis: messageid> <wsadis: To instanceid =/"1234/"> urn: Schemas-xmlsoap-org: WS: 2005: 04: discovery </wsadis: To> "<br/>" <wsadis: Action> http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe </wsadis: Action> "<br/>" </soap: header> <soap: Body> <D: Probe> <D: types> <D: Shawn> <D: yogirl/> </D: Shawn> </D: types> <D: scopes/> </D: Probe> </soap: Body> </soap: envelope> "; <br/> tixmldocument * root = new tixmldocument (); <br/> // root-> LoadFile ("test. XML "); <br/> root-> parse (const char *) BUF); <br/> If (Root = NULL) <br/>{< br/> printf ("Load file failed. /n "); <br/> exit (exit_failure); <br/>}< br/> If (root-> type () = tixmlnode: tinyxml_document) <br/>{< br/> printf ("I Am a document! /N "); <br/>}< br/> tixmlhandle * phead = new tixmlhandle (Root ); <br/> // tixmlnode * pchild = root-> firstchild (); <br/> // tixmlelement * pchild = phead-> firstchild ("Soap: envelope "). firstchild (). firstchild (). element (); <br/> tixmlelement * rootele = root-> rootelement (); <br/> If (rootele) <br/>{< br/> phead-> findchild (rootele, "d: Shawn"); <br/> tixmlelement * pchild = tixmlhandle (phead-> m_plookuproot ). firstchild (). Toelement (); <br/> If (pchild = NULL) <br/>{< br/> printf ("first child is null. /n "); <br/>}< br/> printf (" % s/n ", pchild-> value ()); <br/>}</P> <p> // STD: String wsa_to = "wsadis: To"; <br/> // tixmlstring wsa_to ("wsadis: to "); <br/> // For (; pchild = pchild-> nextsiblingelement ()) <br/> // {<br/> // int type = pchild-> type (); <br/> // switch (type) <br/> // {<br/> // case tixmlnode: tinyxml_el Ement: <br/> // If (wsa_to = pchild-> valuetstr ()) <br/> // {<br/> // printf ("% s/n", pchild-> attribute ("instanceid ")); <br/> //} <br/> // break; <br/> // default: <br/> // break; <br/> //} <br/> If (phead! = NULL) <br/>{< br/> Delete phead; <br/> phead = NULL; <br/>}< br/> If (root! = NULL) <br/>{< br/> Delete root; <br/> root = NULL; <br/>}< br/> # endif <br/> # ifdef testwrite <br/> tixmldocument * Doc = new tixmldocument (); <br/> tixmldeclaration * pdecl = new tixmldeclaration ("1.0", "UTF-8", ""); <br/> tixmlelement * penvelope = new tixmlelement ("Soap: envelope "); <br/> penvelope-> setattribute (" xmlns: Soap "," http://www.w3.org/2003/05/soap-envelope "); <br/> penvelope-> setattribute (" xmlns: D ", "Http://schemas.xmlsoap.org/ws/2005/04/discovery"); <br/> penvelope-> setattribute ("xmlns: wsadis", "http://schemas.xmlsoap.org/ws/2004/08/addressing"); <br/> penvelope-> setattribute ("xmlns: DN ", "http://www.onvif.org/ver10/network/wsdl"); <br/> // header fabricate <br/> tixmlelement * pheader = new tixmlelement ("Soap: Header "); <br/> tixmlelement * pmessageid = new tixmlelement ("wsadis: messageid"); <br/> tixmlt EXT * UUID = new tixmltext ("UUID: blank"); <br/> pmessageid-> linkendchild (UUID); <br/> tixmlelement * PTO = new tixmlelement ("wsadis: to "); <br/> tixmltext * To = new tixmltext (" urn: Schemas-xmlsoap-org: WS: 2005: 04: discovery "); <br/> PTO-> linkendchild (to); <br/> tixmlelement * paction = new tixmlelement ("wsadis: Action "); <br/> tixmltext * Action = new tixmltext ("http://schemas.xmls Response "); <br/> paction-> linkendchild (action); <br/> pheader-> linkendchild (pmessageid); <br/> pheader-> linkendchild (PTO ); <br/> pheader-> linkendchild (paction); <br/> // body fabricate <br/> tixmlelement * pbody = new tixmlelement ("Soap: Body "); <br/> tixmlelement * pprobe = new tixmlelement ("D: probe"); <br/> pbody-> linkendchild (pprobe ); <br/> penvelope-> linkendchild (pheader); <br/> P Envelope-> linkendchild (pbody); <br/> doc-> linkendchild (pdecl); <br/> doc-> linkendchild (penvelope ); <br/> doc-> SaveFile ("send. XML "); <br/> tixmlprinter * printer = new tixmlprinter (); <br/> // printer-> setindent ("/t "); <br/> doc-> Accept (printer); <br/> printf ("% s", printer-> CSTR ()); <br/> // printf ("% s/n", rootelement-> value (); <br/> If (Doc! = NULL) <br/>{< br/> Delete Doc; <br/> Doc = NULL; <br/>}< br/> # endif <br/> getchar (); <br/>}
The findchild () method is implemented by individuals, but frankly speaking, the efficiency is not high. First
The depth of the tree composed of XML files is unknown, and the number of nodes is also unknown, so I think we need to find
Nodes can only be resolved through traversal.
// Added by Shawn. <br/> tixmlhandle: findchild (tixmlelement * _ node, const char * value) <br/>{< br/> If (_ node = 0) <br/>{< br/> return tixmlhandle (0); <br/>}< br/> else if (strcmp (_ node-> value (), value) = 0) <br/>{< br/>/* This is the node we want to find */<br/> m_plookuproot = _ node; <br/> return tixmlhandle (_ node); <br/>}< br/> else <br/>{< br/> printf ("% s/n ", _ node-> value (); <br/> tixmlnode * cur = _ node-> firstchild (); <br/> If (cur) <br/> {<br/> tixmlelement * Next = cur-> toelement (); <br/> for (; next = Next-> nextsiblingelement ()) <br/>{< br/> findchild (next, value); <br/>}< br/> return tixmlhandle (0 ); <br/>}< br/>}
M_plookuproot is the node to be searched ~~~