C/C++解析XML,pugixml库的使用

来源:互联网 发布:二手玫瑰知乎 编辑:程序博客网 时间:2024/05/27 08:13

前言:

本文介绍c++编写的xml解析库——pugixml,能解析xml内容,支持xpath解析,同时能够跨linux平台,非常方便。

总结一下使用步骤和简单的使用方法:

使用pugixml库需要三个文件:pugiconfig.h/pugixml.h/pugixml.cpp,可直接从gugixml官网下载,将其加入工程,使用处如包含头文件pugiconfig.h/pugixml.h。

注意:如果想只是包含头文件pugixml.h,则需要在在pugiconfig.hpp中:
[cpp] view plain copy
  1. // Uncomment this to switch to header-only version  
  2.  #define PUGIXML_HEADER_ONLY  
  3.  #include "pugixml.cpp"  
只需要将这两行的注释去掉即可。
  • 加载xml文件,使用xml_document类的load_file接口:
[cpp] view plain copy
  1. std::strFile = "../test.xml";  
  2. pugi::xml_document doc;  
  3. if (!doc.load_file(strFile.c_str()))   
  4. {  return ;}  
  • 加载xml格式的字符串,使用xml_document类的load接口:
[cpp] view plain copy
  1. std::strText = "testing";  
  2.   pugi::xml_document doc;  
  3.   if (!doc.load(strText.c_str()))   
  4.   {  return ;}  

  • xml节点读取,如:xml文件params.xml:   

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2.   <root>   
  3.    <!-- 输入参数配置 -->   
  4.    <form ip="10.2.134.243" port="80" action="sisserver.php">   
  5.        <input name="data_type" value="POI" />   
  6.        <input name="query_type" value="TQUERY" />   
  7.        <input name="category" value="" />   
  8.    </form> 
  9.    <!-- 查询词的返回结果xpath配置 -->   
  10.    <xpath poiroot="//list/poi" idfield="pguid" namefield="name"/>   
  11.    <!-- 评分权重配置 r1~r4-期望结果的权重,n1~n10-实际查询结果的排名权重-->   
  12.    <weight>   
  13.     <!-- 查询词正常得分阀值 -->   
  14.     <threshold>3</threshold>   
  15.     <!-- 计算分数分布情况的步长值 -->   
  16.     <step>0.5</step>   
  17.    </weight>   
  18.  </root>  

    读取代码:
[cpp] view plain copy
  1. std::string strFile = "/bak/workspace/test/src/params.xml";  
  2.     pugi::xml_document doc;  
  3.     if (!doc.load_file(strFile.c_str()))   
  4.     {return 0;}  
  5.     pugi::xml_node form = doc.child("root").child("form");  
  6.     std::string ip = form.attribute("ip").value();  
  7.     std::string port = form.attribute("port").value();  
  8.      
  9.     char cBuf[2083];  
  10.     sprintf(cBuf, "http://%s:%s/%s?", ip.c_str(), port.c_s());  
  11.     std::string strTemp(cBuf);  
  12.     std::string m_strURLBase = strTemp;  
  13.   
  14.     for (pugi::xml_node input = form.first_child(); input;input = input.next_sibling())  
  15.     {  
  16.         std::string strValue = input.attribute("value").value();  
  17.         if (!strValue.empty())   
  18.         {  
  19.             std::string strName = input.attribute("name").value();  
  20.             sprintf(cBuf, "%s=%s&", strName.c_str(), strValue.c_str());  
  21.             std::string strTemp(cBuf);  
  22.             m_strURLBase += strTemp;  
  23.         }  
  24.     }  
  25.       
  26.     //读取xpath  
  27.     pugi::xml_node xpath = doc.child("root").child("xpath");  
  28.     std::string m_strPOIRoot = xpath.attribute("poiroot").value();  
  29.     std::string m_strPOIID = xpath.attribute("idfield").value();  
  30.   
  31.     //读取评分权重  
  32.     pugi::xml_node weight = doc.child("root").child("weight");  
  33.     float m_fThred = atof(weight.child_value("threshold"));  
  34.     float m_fStep = atof(weight.child_value("step"));  

  • xpath解析,如xml格式的字符串strWebContent:
[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2.    <root>   
  3.     <list count="3" time"10">   
  4.     <poi>  
  5.        <pguid>123</pguid>  
  6.        <name>xx1</name>  
  7.     </poi>   
  8.     <poi>  
  9.        <pguid>456</pguid>  
  10.        <name>xx2</name>  
  11.     </poi>   
  12.     <poi>  
  13.        <pguid>789</pguid>  
  14.        <name>xx3</name>  
  15.     </poi>   
  16.     </list>   
  17.   </root>  

  其中,xpath根路径:m_strPOIRoot="//list/poi", 
  需要取值的项:strPOIID=“pguid”,strPOINam=“name”。

  读取代码:
[cpp] view plain copy
  1. //从strWebContent内容中解析出pguid和name  
  2.  pugi::xml_document doc;  
  3.  pugi::xml_parse_result result = doc.load(strWebContent.c_str());  
  4.  if (!result)  
  5.  {return -1;}  
  6.  pugi::xpath_node_set tools = doc.select_nodes(m_strPOIRoot.c_str());  
  7.  for (pugi::xpath_node_set::const_iterator it = tools.begin();   
  8.      it !=  tools.end(); ++it)  
  9.  {  
  10.     pugi::xpath_node node = *it;  
  11.     string strPOI = node.node().child_value(m_strPOIID.c_str());  
  12.     string strName = node.node().child_value(m_strPOIName.c_str());  
  13.  }  


写入xml方法:

先构造一个结构体

[cpp] view plain copy
  1. struct xml_string_writer : pugi::xml_writer  
  2. {  
  3.     std::string result;  
  4.     virtual void write(const void* data, size_t size)  
  5.     {  
  6.         result += std::string(static_cast<const char*>(data), size);  
  7.     }  
  8. };  

[cpp] view plain copy
  1. void save_xml_config(const std::string &sql_ip, const std::string &sql_port, const std::string& user, const std::string& psw)  
  2. {  
  3.     pugi::xml_document doc;  
  4.     pugi::xml_node pre = doc.prepend_child(pugi::node_declaration);  
  5.     pre.append_attribute("version") = "1.0";  
  6.     pre.append_attribute("encoding") = "UTF-8";  
  7.     pre.append_attribute("standalone") = "no";  
  8.   
  9.     pugi::xml_node node = doc.append_child("sys");  
  10.     pugi::xml_node child_node = node.append_child("item");  
  11.     child_node.append_attribute("name").set_value("mysqlIp");  
  12.     child_node.append_attribute("value").set_value(sql_ip.c_str());  
  13.   
  14.     child_node = node.append_child("item");  
  15.     child_node.append_attribute("name").set_value("mysqlPort");  
  16.     child_node.append_attribute("value").set_value(sql_port.c_str());  
  17.   
  18.     child_node = node.append_child("item");  
  19.     child_node.append_attribute("name").set_value("userName");  
  20.     child_node.append_attribute("value").set_value(user.c_str());  
  21.   
  22.     child_node = node.append_child("item");  
  23.     child_node.append_attribute("name").set_value("userPassword");  
  24.     child_node.append_attribute("value").set_value(psw.c_str());  
  25.     std::string strXmlData;  
  26.     xml_string_writer writer;  
  27.     doc.save(writer);  
  28.     strXmlData = writer.result;  
  29. }  
  30. /************************************************************************/  
  31. /* 
  32. 上述字符串内容显示结果 
  33. <?xml version="1.0" encoding="UTF-8" standalone="no"?> 
  34.   <sys> 
  35.   <item name="mysqlIp" value="127.0.0.1" /> 
  36.   <item name="mysqlPort" value="8888" /> 
  37.   <item name="userName" value="admin" /> 
  38.   <item name="userPassword" value="123465" /> 
  39.   </sys> 
  40. */  
  41. /************************************************************************/  
原创粉丝点击