Tinyxml is a simple, small, C ++ XML parser that can be easily integrated into other programs.
Tinyxml sourcecode: http://sourceforge.net/projects/tinyxml/
Tinyxml only needs to create the DOM tree through first child and next sibling. Then, the whole tree is traversed by using the pointer of the root node as the starting point.
Below is a section of self-operated XMLCode. (C ++)
Typedef struct queueinfo * pqueueinfo; struct queueinfo {int struct; char m_chqueueip [20]; int m_iqueueport; vector <int> m_vctdatatype ;}; typedef struct serverinfo * pserverinfo; struct serverinfo {string m_mongoip; int m_mongoport; string m_mongodbname; double m_mongotimeout; string m_logprefix; string m_logdir; string delimiter; int m_queserverport; int m_queserverid; int m_queservercount; int Interval; vector <queueinfo> m_vctqueueinfo;}; Class serverconf {public: bool readxmlcfg (const char * _ path, pserverinfo _ psi );};
Bool serverconf: readxmlcfg (const char * _ path, pserverinfo _ psi) {tixmldocument * mydocument = new tixmldocument (_ path); If (null = mydocument) {ac_error ("can't init % s", _ path); Return false;} mydocument-> LoadFile (); tixmlelement * proot = mydocument-> rootelement (); if (null = proot) {ac_error ("can't init XML root"); Return false;} // read mongoserver tixmlelement * pmongo = proot-> firstchildeleme NT ("mongodbservercfg"); If (null = pmongo) {ac_error ("can't find queueservicecfg"); Return false ;} _ psi-> m_mongoip = pmongo-> attribute ("serverip"); _ psi-> m_mongoport = atol (pmongo-> attribute ("SERVERPORT ")); _ psi-> m_mongodbname = pmongo-> attribute ("dbname"); _ psi-> m_mongotimeout = atol (pmongo-> attribute ("timeout ")); // read queueserver tixmlelement * pqueues = pmongo-> nextsiblingelement ("queueservercf G "); If (null = pqueues) {ac_error (" can't find queueservicecfg "); Return false ;} _ psi-> m_queserverip = pqueues-> attribute ("serverip"); _ psi-> m_queserverport = atol (pqueues-> attribute ("SERVERPORT ")); _ psi-> m_queserverid = atol (pqueues-> attribute ("ID"); _ psi-> m_queservercount = atol (pqueues-> attribute ("count ")); _ psi-> m_queserverdid = atol (pqueues-> attribute ("did");/* cyclically retrieve queue configuration */tixmlelement * PQ Ueuechild = pqueues-> firstchildelement ("queuecfg"); While (null! = Pqueuechild) {struct queueinfo qiobj; qiobj. m_iqueueid = atoi (pqueuechild-> attribute ("ID");/* ID */memcpy (qiobj. m_chqueueip, pqueuechild-> attribute ("ip"), strlen (pqueuechild-> attribute ("ip");/* IP */qiobj. m_iqueueport = atoi (pqueuechild-> attribute ("Port");/* port */char * strbuf = NULL; char * strtoken; const char * STR = pqueuechild-> attribute ("datatype");/* datatype */string strdatatype (STR ); Strtoken = strtok_r (& strdatatype [0], ",", & strbuf); While (strtoken! = NULL) {qiobj. m_vctdatatype.push_back (atoi (strtoken); strtoken = strtok_r (null, ",", & strbuf);} _ psi-> m_vctqueueinfo.push_back (qiobj ); pqueuechild = pqueuechild-> nextsiblingelement ("queuecfg");} Delete mydocument; return true ;}