pugixml库的使用

来源:互联网 发布:淘宝首页导航条尺寸 编辑:程序博客网 时间:2024/05/20 07:34

pugixml的简单使用

    这两天接触了一个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());
  }


        经过上面的操作可能很多不懂得地方,下面就就需要补充一下pugi的基础了,(其实做项目也是一样,真正遇到了,才会去用某些第三方库,然后才去深入了解她们,后期可以可以添加自己的代码,进行再次封装)。

一、简介

pugixml的官方主页为: http://pugixml.org/

pugixml是一个很棒的XML操作库,

  • 它很轻量,只有三个文件(pugiconfig.hpp   pugixml.cpp  pugixml.hpp )
  • 支持Unicode
  • 支持XPATH解析
  • 速度快,仅比RapidXml慢一些
  • 跨平台(windows/linux)
  • 面向对象

       Xml库解析性能比较表  

(表格来自: http://rapidxml.sourceforge.net/manual.html )

二、配置

pugixml的三个文件,可以只include头文件pugixml.hpp,CPP文件不用放到项目中,

方法是,在pugiconfig.hpp中:

// Uncomment this to switch to header-only version #define PUGIXML_HEADER_ONLY #include "pugixml.cpp"

将这两行的注释去掉就可以了。

另外,如果项目使用的是Unicode设置,则可以在pugiconfig.hpp中:

// Uncomment this to enable wchar_t mode #define PUGIXML_WCHAR_MODE

将wchar模式打开即可。

三、使用

XML文件:

<?xml version="1.0" encoding="GBK"?><root>    <ip>192.168.1.1</ip><root>

C++:

void SaveToConfig( const wchar_t* xml_file, const wchar_t* ip )  {    using namespace pugi;    xml_document doc;    xml_parse_result result = doc.load_file( xml_file );    if ( result.status != xml_parse_status::status_ok )      return;    xml_node node = doc.child( L"root" ).child( L"ip" );    node.text().set( ip );    doc.save_file( xml_file );  }

这里需要注意的是,ip节点的内容是一个pcdata类型的节点,这个节点的内容才是ip字符串,所以这里用text()来读写IP节点内容。

如果要用value()方法得到ip字符串的话,需要这样用:

node.first_child().value();node.first_child().set_value(L"10.10.10.10");

另外,node.text().set()方法也不错,提供了常用的数据类型写入XML的重载方法:

// Set text (returns false if object is empty or there is not enough memory)  bool set(const char_t* rhs);  // Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false")  bool set(int rhs);  bool set(unsigned int rhs);  bool set(double rhs);  bool set(bool rhs);    #ifdef PUGIXML_HAS_LONG_LONG  bool set(long long rhs);  bool set(unsigned long long rhs);    #endif

而node.text().as_xxx()方法可以按需要直接从XML文件中读取出指定类型的数据:

// Get text, or "" if object is empty  const char_t* get() const;  // Get text, or the default value if object is empty  const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const;  // Get text as a number, or the default value if conversion did not succeed or object is empty  int as_int(int def = 0) const;  unsigned int as_uint(unsigned int def = 0) const;  double as_double(double def = 0) const;  float as_float(float def = 0) const;    #ifdef PUGIXML_HAS_LONG_LONG  long long as_llong(long long def = 0) const;  unsigned long long as_ullong(unsigned long long def = 0) const;    #endif

实际上node.text()返回的是xml_text对象实例,上面的set()和as_xxx()是由xml_text实现的。

如果IP节点有属性的话,可以遍历属性:

for (pugi::xml_attribute attr = node.first_attribute(); attr; attr = attr.next_attribute())          {              std::cout << " " << attr.name() << "=" << attr.value();          }  

作为读取配置文件用,上面这些也差不多了,其它接口看看源码就能明白怎样用,pugixml提供了些高级用法,可以看他 官网上提供的例子 。

四、注意事项

除了上面提到的<ip>节点内容为pcdata节点外,

关于中文的问题, clever101 曾在 pugixml库的一个使用心得 中提到,要用

std::locale::global(std::locale("chs"));  const std::wstring strFilePath = _T(“c:\\ xgconsole.xml”);  std::wifstream stream(strFilePath.c_str());  pugi::xml_document doc;  doc.load(stream);  

这种load stream的方式读取,其实不必如此,只要保证文件保存时 编码为GB2312并且XML文件头的声明 encoding="gb2312“ 就可以了。

<?xml version="1.0" encoding="gb2312"?>
0 0
原创粉丝点击