pugixml的简单使用
一、简介
pugixml的官方主页为:http://pugixml.org/
pugixml是一个很棒的XML操作库,
- 它很轻量,只有三个文件(pugiconfig.hpp pugixml.cpp pugixml.hpp )
- 支持Unicode
- 支持XPATH解析
- 速度快,仅比RapidXml慢一些
- 跨平台(windows/linux)
- 面向对象
Xml库解析性能比较表
(表格来自:http://rapidxml.sourceforge.net/manual.html)
2016.12.22更新:
pugixml官网上有了新的性能对比图:
http://pugixml.org/benchmark.html
目前速度上已经比rapid_xml更快。(但我没亲自测试过)
二、配置
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 != status_ok)
return; if (!doc.child( L"root" ))
doc.append_child( L"root" ); xml_node node = doc.child( L"root" ).child( L"ip" );
if (!node)
doc.child( L"root" ).append_child( L"ip" ) node.text().set( ip ); doc.save_file( xml_file );
}
这里需要注意的是,ip节点的内容是一个pcdata类型的节点,这个节点的内容才是ip字符串,所以这里用text()来读写IP节点内容。
如果要用.value()方法得到ip字符串的话,需要这样用:
wstring 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 = ) const;
unsigned int as_uint(unsigned int def = ) const;
double as_double(double def = ) const;
float as_float(float def = ) const; #ifdef PUGIXML_HAS_LONG_LONG
long long as_llong(long long def = ) const;
unsigned long long as_ullong(unsigned long long def = ) 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();
}
C++11:
for (pugi::xml_attribute attr : node.attributes())
{
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"?>
pugixml的简单使用的更多相关文章
- pugixml简单实用
实现快递查询,调用快递100的API,未完成. #include <iostream> #include <fstream> #include <string> # ...
- Pugixml一种快速解析XML文件的开源解析库
Pugixml是一个轻量级的C++ XML开源解析库,DOM形式的解析器.接口和丰富的遍历和修改操作,快速的解析,此外支持XPath1.0实现数据查询,支持unicode编码: 使用Pugixml可通 ...
- pugixml使用教程
pugixml介绍 pugixml是一个高性能.轻量级并且简单易用的xml解析库,支持UTF8 encoding.Little-endian UTF16.Big-endian UTF16.UTF16 ...
- [转]pugixml使用教程
转自:https://www.cnblogs.com/ltm5180/p/3989125.html pugixml介绍 pugixml是一个高性能.轻量级并且简单易用的xml解析库,支持UTF8 en ...
- pugixml应用随笔
1. 修改元素值 second_node.set_value("miller");不对 必须second_node.first_child().set_value(" ...
- 【造轮子】打造一个简单的万能Excel读写工具
大家工作或者平时是不是经常遇到要读写一些简单格式的Excel? shit!~很蛋疼,因为之前吹牛,就搞了个这东西,还算是挺实用,和大家分享下. 厌烦了每次搞简单类型的Excel读写?不怕~来,喜欢流式 ...
- Fabio 安装和简单使用
Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- 哪种缓存效果高?开源一个简单的缓存组件j2cache
背景 现在的web系统已经越来越多的应用缓存技术,而且缓存技术确实是能实足的增强系统性能的.我在项目中也开始接触一些缓存的需求. 开始简单的就用jvm(java托管内存)来做缓存,这样对于单个应用服务 ...
随机推荐
- 7.jenkins 按标签发布
jenkins 如果要按标签发布,需要安装下, Git Parameter Plug-In 的 插件. 之前我们的jar包项目. 我们运行的时候是以下内容. 现在我们对这个jar进行小范围修改. ...
- LG2145 「JSOI2007」祖码 区间DP
问题描述 LG2145 题解 把颜色相同的一段看做一个点. 然后类似于合唱队区间DP即可. 但是这题好像出过一些情况,导致我包括题解区所有人需要特判最后一个点. \(\mathrm{Code}\) # ...
- 【python爬虫】正则表达式
一.数据的分类 1.结构化数据 特点:数据以行为单位,每一个数据表示一个实体.每一行数据的属性都是一样的. 举例:关系型数据库中的表就是结构化数据. 处理方法:sql 2.半结构化数据 特点:结构化数 ...
- ROS下多雷达融合算法
有些小车车身比较长,如果是一个激光雷达,顾前不顾后,有比较大的视野盲区,这对小车导航定位避障来说都是一个问题,比如AGV小车, 所有想在小车前后各加一个雷达,那问题是ROS的建图或者定位导航都只是支持 ...
- 【MySQL报错】ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot exec ...
- laravel中的表单请求类型和CSRF防护(六)
laravel中为我们提供了绑定不同http请求类型的函数. Route::get('/test', function () {}); Route::post('/test', function () ...
- wp.editor.initialize 配置案例
wp.editor.initialize ( 'EditorTextArea' , { tinymce: { wpautop: to true , theme: 'modern' , skin: 'l ...
- .NET MVC5简介(五)管道处理模型IHttpModule
https://www.cnblogs.com/JimmyZhang/archive/2007/09/04/880967.html IHttpModule HTTPRuntime(运行时).在一个控制 ...
- 湖南省web应用软件(中慧杯)
湖南省web应用软件 写这篇博客已经是比完赛的第四天了,我还记得那天下着小雨.我们早早的到了比赛的现场抽检机器,在比赛前一天我很是激动.我还记得我们从学校,去株洲的时候我们的领导来给我加油,特别是我的 ...
- java基础(25):Properties、序列化流、打印流、commons-IO
1. Properties类 1.1 Properties类介绍 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符 ...