pugixml的简单使用
(2) 加载xml文件,使用xml_document类 的load_file接口:
(3) 加载xml格式的字符串,使用xml_document类的load接口:
(4) xml节点读取,如xml文件params.xml:
(5) xpath解析,如xml格式的字符串strWebContent:
一、简介
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中:
<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">// Uncomment this to switch to header-only version</span> <span class="preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#<span class="keyword" style="color: rgb(51, 51, 51);">define</span> PUGIXML_HEADER_ONLY</span> <span class="preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#include "pugixml.cpp"</span>
将这两行的注释去掉就可以了。
另外,如果项目使用的是Unicode设置,则可以在pugiconfig.hpp中:
<span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">// Uncomment this to enable wchar_t mode</span> <span class="preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#<span class="keyword" style="color: rgb(51, 51, 51);">define</span> PUGIXML_WCHAR_MODE</span>
将wchar模式打开即可。
三、使用
XML文件:
<span style="color: rgb(0, 0, 255);"><span class="pi" style="color: rgb(153, 153, 153); font-weight: bold;"><?</span></span><span class="pi" style="color: rgb(153, 153, 153); font-weight: bold;"><span style="color: rgb(255, 0, 255);">xml version="1.0" encoding="GBK"</span><span style="color: rgb(0, 0, 255);">?></span></span><span style="color: rgb(0, 0, 255);"></span> <span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"><</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">root</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span> <span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"><</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">ip</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>192.168.1.1<span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"></</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">ip</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span> <span style="color: rgb(0, 0, 255);"><span class="tag" style="color: rgb(0, 0, 128);"><</span></span><span class="tag" style="color: rgb(0, 0, 128);"><span style="color: rgb(128, 0, 0);"><span class="title" style="color: rgb(0, 0, 128);">root</span></span><span style="color: rgb(0, 0, 255);">></span></span><span style="color: rgb(0, 0, 255);"></span>
C++:
<span class="keyword" style="font-weight: bold;">void</span> SaveToConfig( <span class="keyword" style="font-weight: bold;">const</span> <span class="keyword" style="font-weight: bold;">wchar_t</span>* xml_file, <span class="keyword" style="font-weight: bold;">const</span> <span class="keyword" style="font-weight: bold;">wchar_t</span>* ip )
<span class="indent"> </span>{
<span class="indent"> </span><span class="indent"> </span><span class="keyword" style="font-weight: bold;">using</span> <span class="keyword" style="font-weight: bold;">namespace</span> pugi;
<span class="indent"> </span><span class="indent"> </span>xml_document doc;
<span class="indent"> </span><span class="indent"> </span>xml_parse_result result = doc.load_file( xml_file );
<span class="indent"> </span><span class="indent"> </span><span class="keyword" style="font-weight: bold;">if</span> ( result.status != xml_parse_status::status_ok )
<span class="indent"> </span><span class="indent"> </span><span class="indent"> </span><span class="keyword" style="font-weight: bold;">return</span>;
<span class="indent"> </span><span class="indent"> </span>xml_node node = doc.child( L<span class="string" style="color: rgb(221, 17, 68);">"root"</span> ).child( L<span class="string" style="color: rgb(221, 17, 68);">"ip"</span> );
<span class="indent"> </span><span class="indent"> </span>node.text().<span class="built_in" style="color: rgb(0, 134, 179);">set</span>( ip );
<span class="indent"> </span><span class="indent"> </span>doc.save_file( xml_file );
<span class="indent"> </span>}
这里需要注意的是,ip节点的内容是一个pcdata类型的节点,这个节点的内容才是ip字符串,所以这里用text()来读写IP节点内容。
如果要用value()方法得到ip字符串的话,需要这样用:
node.first_child().<span class="keyword" style="font-weight: bold;">value</span>(); node.first_child().set_value(L<span class="string" style="color: rgb(221, 17, 68);">"10.10.10.10"</span>);
另外,node.text().set()方法也不错,提供了常用的数据类型写入XML的重载方法:
<span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> <span class="operator"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">Set</span> text (returns <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">false</span> if object <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">is</span> empty <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">or</span> there <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">is</span> <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">not</span> enough memory)</span></span><span class="operator">
<span class="indent"> </span><span style="color: rgb(0, 0, 255);">bool</span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">set</span></span>(<span style="color: rgb(0, 0, 255);">const</span> char_t*<span style="color: rgb(0, 0, 0);"> rhs);</span></span><span style="color: rgb(0, 0, 0);">
<span class="indent"> </span></span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> <span class="operator"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">Set</span> text <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">with</span> type conversion (numbers <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">are</span> converted <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">to</span> strings, boolean <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">is</span> converted <span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">to</span> <span class="string" style="color: rgb(221, 17, 68);">"true"</span>/<span class="string" style="color: rgb(221, 17, 68);">"false"</span>)</span></span><span class="operator">
<span class="indent"> </span><span style="color: rgb(0, 0, 255);">bool</span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">set</span></span>(<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">int</span></span><span style="color: rgb(0, 0, 0);"> rhs);</span></span><span style="color: rgb(0, 0, 0);">
<span class="indent"> </span></span><span style="color: rgb(0, 0, 255);">bool</span> <span style="color: rgb(0, 0, 255);"><span class="operator"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">set</span></span></span><span class="operator">(unsigned <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">int</span></span><span style="color: rgb(0, 0, 0);"> rhs);</span></span><span style="color: rgb(0, 0, 0);">
<span class="indent"> </span></span><span style="color: rgb(0, 0, 255);">bool</span> <span style="color: rgb(0, 0, 255);"><span class="operator"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">set</span></span></span><span class="operator">(<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">double</span></span><span style="color: rgb(0, 0, 0);"> rhs);</span></span><span style="color: rgb(0, 0, 0);">
<span class="indent"> </span></span><span style="color: rgb(0, 0, 255);">bool</span> <span style="color: rgb(0, 0, 255);"><span class="operator"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">set</span></span></span><span class="operator">(<span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> rhs);</span></span><span style="color: rgb(0, 0, 0);">
#ifdef PUGIXML_HAS_LONG_LONG
<span class="indent"> </span></span><span style="color: rgb(0, 0, 255);">bool</span> <span style="color: rgb(0, 0, 255);"><span class="operator"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">set</span></span></span><span class="operator">(<span style="color: rgb(0, 0, 255);">long</span> <span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> rhs);</span></span><span style="color: rgb(0, 0, 0);">
<span class="indent"> </span></span><span style="color: rgb(0, 0, 255);">bool</span> <span style="color: rgb(0, 0, 255);"><span class="operator"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">set</span></span></span><span class="operator">(unsigned <span style="color: rgb(0, 0, 255);">long</span> <span style="color: rgb(0, 0, 255);">long</span><span style="color: rgb(0, 0, 0);"> rhs);</span></span><span style="color: rgb(0, 0, 0);">
</span><span style="color: rgb(0, 0, 255);">#endif</span>
而node.text().as_xxx()方法可以按需要直接从XML文件中读取出指定类型的数据:
<span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);"> Get text, or "" if object is empty</span></span><span style="color: rgb(0, 128, 0);"></span>
<span class="indent"> </span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span> char_t* <span style="color: rgb(0, 0, 255);">get</span>() <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span><span style="color: rgb(0, 0, 0);">;
<span class="indent"> </span></span><span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);"> Get text, or the default value if object is empty</span></span><span style="color: rgb(0, 128, 0);"></span>
<span class="indent"> </span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span> char_t* as_string(<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span> char_t* def = PUGIXML_TEXT(<span style="color: rgb(128, 0, 0);"><span class="string" style="color: rgb(221, 17, 68);">""</span></span>)) <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span><span style="color: rgb(0, 0, 0);">;
<span class="indent"> </span></span><span style="color: rgb(0, 128, 0);"><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;">//</span></span><span class="comment" style="color: rgb(153, 153, 136); font-style: italic;"><span style="color: rgb(0, 128, 0);"> Get text as a number, or the default value if conversion did not succeed or object is empty</span></span><span style="color: rgb(0, 128, 0);"></span>
<span class="indent"> </span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">int</span></span> as_int(<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">int</span></span> def = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">0</span></span>) <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span><span style="color: rgb(0, 0, 0);">;
<span class="indent"> </span><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">unsigned</span> </span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">int</span></span> as_uint(<span class="keyword" style="font-weight: bold;">unsigned</span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">int</span></span> def = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">0</span></span>) <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span><span style="color: rgb(0, 0, 0);">;
<span class="indent"> </span></span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">double</span></span> as_double(<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">double</span></span> def = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">0</span></span>) <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span><span style="color: rgb(0, 0, 0);">;
<span class="indent"> </span></span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">float</span></span> as_float(<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">float</span></span> def = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">0</span></span>) <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span><span style="color: rgb(0, 0, 0);">;
<span class="preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#ifdef PUGIXML_HAS_LONG_LONG</span>
<span class="indent"> </span></span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span></span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span></span> as_llong(<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span></span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span></span> def = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">0</span></span>) <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span><span style="color: rgb(0, 0, 0);">;
<span class="indent"> </span><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">unsigned</span> </span><span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span></span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span></span> as_ullong(<span class="keyword" style="font-weight: bold;">unsigned</span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span></span> <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span></span> def = <span style="color: rgb(128, 0, 128);"><span class="number" style="color: rgb(0, 153, 153);">0</span></span>) <span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">const</span></span><span style="color: rgb(0, 0, 0);">;
</span><span style="color: rgb(0, 0, 255);"><span class="preprocessor" style="color: rgb(153, 153, 153); font-weight: bold;">#endif</span></span>
实际上node.text()返回的是xml_text对象实例,上面的set()和as_xxx()是由xml_text实现的。
如果IP节点有属性的话,可以遍历属性:
<span style="color: rgb(0, 0, 255);"><span class="keyword" style="color: rgb(51, 51, 51); font-weight: bold;">for</span></span> (pugi::xml_attribute attr = node.first_attribute(); attr; attr =<span style="color: rgb(0, 0, 0);"> attr.next_attribute())
{
std::cout </span><<span class="xml"><span class="tag" style="color: rgb(0, 0, 128);">< <span style="color: rgb(128, 0, 0);">"</span> <span style="color: rgb(128, 0, 0);">"</span> << <span class="attribute" style="color: rgb(0, 128, 128);">attr.name</span>() << <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">=</span><span style="color: rgb(128, 0, 0);"><span class="value" style="color: rgb(221, 17, 68);">"</span></span><span class="value" style="color: rgb(221, 17, 68);"> <<<span style="color: rgb(0, 0, 0);"> attr.value();
} </span></span><span style="color: rgb(0, 0, 0);"></span></span><span style="color: rgb(0, 0, 0);"></span></span><span style="color: rgb(0, 0, 0);"></span>
作为读取配置文件用,上面这些也差不多了,其它接口看看源码就能明白怎样用,pugixml提供了些高级用法,可以看他 官网上提供的例子 。
四、注意事项
除了上面提到的<ip>节点内容为pcdata节点外,
关于中文的问题, clever101 曾在 pugixml库的一个使用心得 中提到,要用
<span class="symbol" style="color: rgb(153, 0, 115);">std:</span><span class="symbol" style="color: rgb(153, 0, 115);">:locale</span><span class="symbol" style="color: rgb(153, 0, 115);">:</span><span class="symbol" style="color: rgb(153, 0, 115);">:<span style="color: rgb(0, 0, 255);">global</span></span><span style="color: rgb(0, 0, 255);"></span>(<span class="symbol" style="color: rgb(153, 0, 115);">std:</span><span class="symbol" style="color: rgb(153, 0, 115);">:locale</span>(<span style="color: rgb(128, 0, 0);"><span class="string" style="color: rgb(221, 17, 68);">"</span></span><span class="string" style="color: rgb(221, 17, 68);"><span style="color: rgb(128, 0, 0);">chs</span><span style="color: rgb(128, 0, 0);">"</span></span><span style="color: rgb(128, 0, 0);"></span><span style="color: rgb(0, 0, 0);">));
</span><span style="color: rgb(0, 0, 255);">const</span> <span class="symbol" style="color: rgb(153, 0, 115);">std:</span><span class="symbol" style="color: rgb(153, 0, 115);">:wstring</span> strFilePath =<span style="color: rgb(0, 0, 0);"> _T(“<span class="symbol" style="color: rgb(153, 0, 115);">c:</span>\\ xgconsole.xml”);
<span class="symbol" style="color: rgb(153, 0, 115);">std:</span><span class="symbol" style="color: rgb(153, 0, 115);">:wifstream</span> stream(strFilePath.c_str());
<span class="symbol" style="color: rgb(153, 0, 115);">pugi:</span><span class="symbol" style="color: rgb(153, 0, 115);">:xml_document</span> doc;
doc.load(stream); </span>
这种load stream的方式读取,其实不必如此,只要保证文件保存时 编码为GB2312并且XML文件头的声明 encoding="gb2312“ 就可以了。
<span class="pi" style="color: rgb(153, 153, 153); font-weight: bold;"><?xml version="1.0" encoding="gb2312"?></span>
<span style="color:#999999;"><strong>转自:http://blog.csdn.net/u011090298/article/details/45726523</strong></span>