We recommend an efficient CPP parsing XML tool-rapidxml

Source: Internet
Author: User
Tags xml parser

The parsing efficiency is 50-times faster than xerces Dom, and tinyxml is 30-60. The author said that this is the fastest XML parsing library he knows ~~

The author introduced: "The table below compares speed of rapidxml to some other parsers, And to strlen () function executed on the same data. on a modern CPU (as of 2007), You can reset CT parsing throughput to be close to 1 Gb/s. as a rule of thumb, parsing speed is about 50-100x faster than xerces Dom, 30-60x faster than tinyxml, 3-12x faster than pugxml, and about 5%-30% faster than pugixml, the fastest XML Parser I know."

 
Address: http://rapidxml.sourceforge.net/

Specific introduction can look at here: http://rapidxml.sourceforge.net/manual.html

Let's take an example to see how this tool works.

XML file:

<?xml version="1.0" encoding="utf-8" ?><root><title id="1" qnums="1"><name  color="255,255,50" fontsize="16" >TEST</name><question type="math" gentype="auto" desc="because 1+1=2" >  <![CDATA[function matchwo(a,b){if (a < b && a < 0) then  {  return 1;  }else  {  return 0;  }}]]></question></title></root>

Source code:

// Example. CPP: defines the entry point for the console application. // # include "stdafx. H "# include" rapidxml/rapidxml. HPP "# include" vector "# include" windows. H "# include" iostream "using namespace STD; using namespace rapidxml; # include" stdio. H "# define get_attribute (pretval, node, Val) xml_attribute <> * pattr # pretval = (node)-> first_attribute (VAL )); \ If (pattr # pretval) {(pretval) = pattr # pretval-> value ();} /* Only applicable when parse_default is used for parsing. */Const char * getnodevalue (xml_node <> * node) {If (node = NULL) {return "" ;}if (node-> value ()! = NULL & (node-> value () [0]! = '\ 0') {return node-> value ();} xml_node <> * cdatanode = node-> first_node (); If (cdatanode! = NULL & cdatanode-> type () = node_cdata) {return cdatanode-> value ();} return "" ;}/ * parse_default: to improve efficiency, raw data is changed. The ending character \ 0parse_non_destructive is added to each attribute value: raw data is not changed */INT parsedataxml (const char * szxml) {try {xml_document <char> Doc; // character type defaults to chardoc. parse <parse_default> (char *) szxml); // parse_non_destructive xml_node <> * root = Doc. first_node (); // get the xml_node with the node <> * titlenode = root-> first_n Ode ("title"); If (titlenode) {xml_node <> * namenode = titlenode-> first_node ("name"); string strname; strname = getnodevalue (namenode ); printf ("strname [% s] \ n", strname. c_str (); string color; char * pcolor = NULL; get_attribute (pcolor, namenode, "color"); printf ("pcolor [% s] \ n ", pcolor = NULL? "": Pcolor) ;}} catch (parse_error & E) {printf ("% s \ n", E. what ();} return 1;} int _ tmain (INT argc, _ tchar * argv []) {file * fp = NULL; FP = fopen ("data. XML "," rb "); char Buf [1024]; string xml =" "; while (fgets (BUF, 1024, FP )! = NULL) {XML + = Buf;} fclose (FP); parsedataxml (XML. c_str ();} getchar (); return 1 ;}

Output:

 

This open-source library is lightweight and the DOM model is clearly stated. I can't find the disadvantages, because the project relationship mainly uses it for resolution, so I still encapsulate it with several more interfaces:

Int getxmlattrvalue (xml_node <> * node, const char * val, STD: string & strval );

Int getxmlnodevalue (xml_node <> * node, string & retval );

Int getxmlchildnodevalue (const xml_node <> * rootnode, const char * pszchildstr, string & strret );

Int getxmlnode (const xml_node <> * rootnode, str_vector & vecstr, xml_node <> * & stretnode );

 

 

Source code download

Sorry for the error ~~ Welcome ~~~

 

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.