pugixml庫的使用(by yukin_xue)

來源:互聯網
上載者:User

      這兩天接觸了一個c++編寫的xml解析庫——pugixml,能解析xml內容,支援xpath解析,且能跨linux平台,不錯!以前一直習慣用CMarkup,主要用它讀寫xml設定檔,但CMarkup不支援xpath,也只能在windows用,雖然習慣了CMarkup,不過若需要xpath解析,又需要跨linux平台,相比之下,pugixml確實是很好的選擇,操作速度也快。學習文檔:http://pugixml.googlecode.com/svn/tags/latest/docs/quickstart.html
, 總結一下使用步驟和簡單的使用方法:

(1)使用pugixml庫需要三個檔案:pugiconfig.h/pugixml.h/pugixml.cpp,可直接從gugixml官網下載,將其加入工程,使用處包含標頭檔pugiconfig.h/pugixml.h即可。

(2)載入xml檔案,使用xml_document類的load_file介面:
    std::strFile = "../test.xml";
    pugi::xml_document doc;
    if (!doc.load_file(strFile.c_str()))
    {  //return -1;}

(3)載入xml格式的字串,使用xml_document類的load介面:
    std::strText = "****";
    pugi::xml_document doc;
    if (!doc.load(strText.c_str()))
    {  //return -1;}

(4)xml節點讀取,如xml檔案params.xml:
    <?xml version="1.0" encoding="utf-8" ?>  
   <root>
    <!-- 輸入參數配置 -->
    <form ip="10.2.134.243" port="80" action="sisserver.php">
    <input name="data_type" value="POI" />
    <input name="query_type" value="TQUERY" />
    <input name="category" value="" />
 
    <!-- 查詢詞的返回結果xpath配置 -->
    <xpath poiroot="//list/poi" idfield="pguid" namefield="name"/>
    <!-- 評分權重配置 r1~r4-期望結果的權重,n1~n10-實際查詢結果的排名權重-->
    <weight>
     <!-- 查詢詞正常得分閥值 -->
     <threshold>3</threshold>
     <!-- 計算分數分布情況的步長值 -->
     <step>0.5</step>
    </weight>
  </root>
    讀取代碼:
    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;
    }
    }
    
    //讀取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();

    //讀取評分權重
    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解析,如xml格式的字串strWebContent:
 <?xml version="1.0" encoding="utf-8" ?>  
   <root>
    <list count="3" time"10">
    <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>
  其中,xpath根路徑:m_strPOIRoot="//list/poi",
  需要取值的項:strPOIID=“pguid”,strPOINam=“name”。

  讀取代碼:
  //從strWebContent內容中解析出pguid和name
  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());
  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.