Pugixml self-Used XPath

Source: Internet
Author: User

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

(2) Load the XML file, using 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) Load the XML-formatted string using the Xml_document class's load interface:
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=" >
<input name= "data_type" value= "POI"/>
<input name= "Query_type" value= "Tquery"/>
<input name= "category" Value= ""/>

<!--The return result of the query word XPath configuration--
<xpath poiroot= "//list/poi" idfield= "Pguid" namefield= "name"/>
<!--scoring weights configure r1~r4-The expected results, n1~n10-the actual query results by the ranking weights--
<weight>
<!--query terms normal score threshold--
<threshold>3</threshold>
<!--the step value for calculating the distribution of fractions--
<step>0.5</step>
</weight>
</root>
To read the 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 rating 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 XML-formatted string strwebcontent:
<?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 that need to be valued: strpoiid= "Pguid", strpoinam= "name".

To read the code:
Parse the Pguid and name from the 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 ());

}


/////////////////////////////////////////////////////////////

Pugi::xml_document Doc;

if (!doc.load_file ("E:\\code\\windows\\iqsmgr\\debug\\xgconsole.xml"))
{
return;
}
Select nodes via compiled query
Pugi::xpath_query query_remote_tools ("/bookstore/book");



Pugi::xpath_node_set tools = Doc.select_nodes ("/bookstore/book");


int n = tools.size ();
std::string M_strnameatt = "title";
std::string m_strnameatt1 = "Price";


for (Pugi::xpath_node_set::const_iterator it = Tools.begin (); It! = Tools.end (); ++it)
{
Pugi::xpath_node node = *it;
std::string Strpoi = Node.node (). Child_value (M_strnameatt.c_str ());
std::string strName = Node.node (). Child_value (M_strnameatt1.c_str ());
}
/////////////////////////////////////////////////////////////



Waiting for implementation

///////////////////////////////////////

Pugi::xml_document Doc;

///////////////////
if (!doc.load_file ("Xgconsole.xml")) return-1;


Pugi::xml_node tools = Doc.child ("Profile"). Child ("Tools");


[Code_traverse_base_basic
for (Pugi::xml_node tool = Tools.first_child (); tool; tool = Tool.next_sibling ())
{
Std::cout << "Tool:";


for (Pugi::xml_attribute attr = Tool.first_attribute (); attr; attr = Attr.next_attribute ())
{
Std::cout << "" << attr.name () << "=" << attr.value ();
}


Std::cout << Std::endl;
}
//]

//////////////////////////////

<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
<profile formatversion= "1" >
<Tools>
<tool filename= "Jam" allowintercept= "true" >
<description>jamplus Build System</description>
</Tool>
<tool filename= "Mayabatch.exe" allowremote= "true" outputfilemasks= "*.dae" derivecaptionfrom= "Lastparam" Timeout= "/>"
<tool filename= "Meshbuilder_*.exe" allowremote= "false" outputfilemasks= "*.mesh" derivecaptionfrom= "Lastparam" timeout= "Ten"/>
<tool filename= "Texbuilder_*.exe" allowremote= "true" outputfilemasks= "*.tex" derivecaptionfrom= "Lastparam"/>
<tool filename= "Shaderbuilder_*.exe" allowremote= "true" derivecaptionfrom= "Lastparam"/>
</Tools>
</Profile>

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.