C + + cross-platform XML parsing class library-pugixml__c++

Source: Internet
Author: User
Tags sprintf xpath
use of the Pugixml library

This two-day contact with a C + + written XML parsing library--pugixml, can parse XML content, support XPath parsing, and across the Linux platform, good. Previously used to use CMarkup, mainly read and write XML configuration files, but CMarkup does not support XPath, but also only in windows, although accustomed to cmarkup, but if you need XPath parsing, and need to cross the Linux platform, by contrast, Pugixml is really a good choice, and the speed of operation is also fast. Learn the Documentation: http://pugixml.googlecode.com/svn/tags/latest/docs/quickstart.html, summarize the use steps and simple ways to use: (1) The use of Pugixml library requires three files: Pugiconfig.h/pugixml.h/pugixml.cpp, can be directly downloaded from the Gugixml official website, add it to the project, the use of a header file pugiconfig.h/pugixml.h can be.

(2) Load the XML file and use the Load_file interface of the Xml_document class:
Std::strfile = ".. /test.xml ";
Pugi::xml_document Doc;
if (!doc.load_file (Strfile.c_str ()))
{//return-1;}

(3) Loading XML-formatted strings, using the load interface of the Xml_document class:
Std::strtext = "* * *";
Pugi::xml_document Doc;
if (!doc.load (Strtext.c_str ()))
{//return-1;}

(4) XML node reads, such as XML file Params.xml:
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<!--input parameter configuration-->
<form ip= "10.2.134.243" port= "action=" "sisserver.php" >
<input name= "data_type" value= "POI"/>
<input name= "Query_type" value= "Tquery"/>
<input name= "category" Value= ""/>

<!--return results of query words XPath configuration-->
<xpath poiroot= "//list/poi" idfield= "Pguid" namefield= "name"/>
<!--weight allocation r1~r4-The weight of the expected result, n1~n10-the ranking weight of the actual query results-->
<weight>
<!--query Word normal score threshold-->
<threshold>3</threshold>
<!--the step value of calculating fractional distribution-->
<step>0.5</step>
</weight>
</root>
Read code:
std::string strfile = "/bak/workspace/test/src/params.xml";
Pugi::xml_document Doc;
if (!doc.load_file (Strfile.c_str ()))
{return 0;}
Pugi::xml_node form = doc.child ("root"). Child ("form");
std::string IP = form.attribute ("IP"). Value ();
std::string port = form.attribute ("port"). Value ();

Char cbuf[2083];
sprintf (Cbuf, "http://%s:%s/%s", Ip.c_str (), port.c_s ());
std::string strtemp (CBUF);
std::string m_strurlbase = strtemp;

for (Pugi::xml_node input = Form.first_child (); input;
input = input.next_sibling ())
{
std::string strvalue = Input.attribute ("value"). Value ();
if (!strvalue.empty ())
{
std::string strName = Input.attribute ("name"). Value ();
sprintf (Cbuf, "%s=%s&", Strname.c_str (), Strvalue.c_str ());
std::string strtemp (CBUF);
M_strurlbase + = strtemp;
}
}

Read XPath
Pugi::xml_node XPath = doc.child ("root"). Child ("XPath");
std::string m_strpoiroot = Xpath.attribute ("Poiroot"). Value ();
std::string m_strpoiid = Xpath.attribute ("IDfield"). Value ();

Read score Weights
Pugi::xml_node weight = doc.child ("root"). Child ("weight");
float m_fthred = atof (Weight.child_value ("threshold"));
float M_fstep = atof (Weight.child_value ("step"));

(5) XPath parsing, such as String strwebcontent in XML format:
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<list count= "3" Time "ten" >
<poi>
<pguid>123</pguid>
<name>xx1</name>
</poi>
<poi>
<pguid>456</pguid>
<name>xx2</name>
</poi>
<poi>
<pguid>789</pguid>
<name>xx3</name>
</poi>
</list>
</root>
Where the XPath root path: m_strpoiroot= "//list/poi",
Items to be evaluated: strpoiid= "Pguid", strpoinam= "name".

Read code:
Parse out Pguid and name from strwebcontent content
Pugi::xml_document Doc;
Pugi::xml_parse_result result = Doc.load (Strwebcontent.c_str ());
if (!result)
{return-1;}
Pugi::xpath_node_set tools = Doc.select_nodes (M_strpoiroot.c_str ());
for (Pugi::xpath_node_set::const_iterator it = Tools.begin ();
It!= tools.end (); ++it)
{
Pugi::xpath_node node = *it;
String strpoi = Node.node (). Child_value (M_strpoiid.c_str ());
String strName = Node.node (). Child_value (M_strpoiname.c_str ());
}

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.