以前都是用tinyxml,这次开发中解析xml配置文件像尝试一下rapidxml,据说效率很高。。。

RapidXml Manual: http://rapidxml.sourceforge.net/manual.html

RapidXml是一个使用C++编写的XML DOM解析工具包,整个解析工具包包含在一个头文件中,所以使用时不用编译也不用连接。只要包含rapidxml中的三个头文件即可。

RapidXml 试图成为最快的 XML DOM 解析工具包,同时保证解析结果的可用性、可移植性以及与 W3C 标准的兼容性。在操作同一数据时,其解析速度接近于 strlen() 函数。

以下代码使用RapidXml解析一段以0结束的字符串text:

1 using namespace rapidxml;
2 xml_document<> doc; // character type defaults to char
3 doc.parse<0>(text); // 0 means default parse flags

其中,doc为解析得到的DOM tree的根节点。由于所有的RapidXml接口都包含在rapixml,所以用户需要使用这个名字空间。类xml_document代表了DOM结构的根,它公开继承了xml_node和memory_pool。xml_document::parse()的模板参数用来标识解析标志,使用它可以对解析器的行为进行调整(这里我也不太明白,调整什么?)。这个标志必须是编译时的常数。

  • Accessing DOM Tree:

使用xml_node和xml_attribute类中的方法访问DOM tree。

1 cout << "Name of my first node is: " << doc.first_node()->name() << "\n";
2 xml_node<> *node = doc.first_node("foobar");
3 cout << "Node foobar has value " << node->value() << "\n";
4 for (xml_attribute<> *attr = node->first_attribute();
5 attr; attr = attr->next_attribute())
6 {
7 cout << "Node foobar has attribute " << attr->name() << " ";
8 cout << "with value " << attr->value() << "\n";
9 }
  • Modifying DOM Tree:

下例为创建一个HTML文档,它唯一的内容是一个google.com的链接( <a href=google.com>Google</a>):

xml_document<> doc;
xml_node<> *node = doc.allocate_node(node_element, "a", "Google");
doc.append_node(node);
xml_attribute<> *attr = doc.allocate_attribute("href", "google.com");
node->append_attribute(attr);

nodes和attributes并不真正拥有文章中节点和属性的名字及值,因为它们只是存储了指向源文中某个位置的指针。所以,当为一个节点分配名字和值的时候,必须确保待这些字符串有合适的生命周期。最简单的方法是从xml_document memory pool中分配字符串。当然,在上面例子中没有必要这么做,因为这里使用了字符常量。下面的代码使用了memory_pool::allocate_string()方法分配节点名字(这样它将和文档具有相同的生命周期)给新的节点:

xml_document<> doc;
char *node_name = doc.allocate_string(name); // Allocate string and copy name into it
xml_node<> *node = doc.allocate_node(node_element, node_name); // Set node name to node_name
  • Printing XML

将xml_document和xml_node的对象写入一XML的string里,可以使用在rapidxml_print.hpp头文件中定义的print()函数或者操作符<<。

using namespace rapidxml;
xml_document<> doc; // character type defaults to char
// ... some code to fill the document // Print to stream using operator <<
std::cout << doc; // Print to stream using print function, specifying printing flags
print(std::cout, doc, 0); // 0 means default printing flags // Print to string using output iterator
std::string s;
print(std::back_inserter(s), doc, 0); // Print to memory buffer using output iterator
char buffer[4096]; // You are responsible for making the buffer large enough!
char *end = print(buffer, doc, 0); // end contains pointer to character after last printed character
*end = 0; // Add string terminator after XML

包含必要的头文件
#include "rapidxml.hpp"  
创建文档对象
rapidxml::xml_document<char> doc;  
分析xml字符串,要求以'\0'结尾
std::string str(...);
doc.parse<0>(const_cast<char *>(str.c_str()));  
获取节点
rapidxml::xml_node<char> * node = doc.first_node("node name");  
遍历所有节点
for(rapidxml::xml_node<char> * node = parent_node->first_node("node name");
    node != NULL;
    node = node->next_sibling())
{
    ...
}
  
遍历所有属性
for(rapidxml::xml_attribute<char> * attr = node->first_attribute("node name");
    attr != NULL;
    attr = attr->next_attribute())
{
    ...
}  
获取属性值
char * value = attr->value();

  1. // Load file to buffer.
  2. rapid::xml_file<> fileLoad("test.xml");
  3. char* content = fileLoad.data();
  4. if (content) {
  5. // Load content to rapid.
  6. rapid::xml_document<> docParse;
  7. docParse.parse<0>(content);
  8. // Parse node.
  9. rapid::xml_node<>* node_root = docParse.first_node("item");
  10. ...
  11. }
  1. rapid::xml_docmenet<> docSave;
  2. // Generate root node.
  3. rapid::xml_node<>* node_root = docSave.alloc_node(docSave.allocate_string("item"));
  4. ...
  5. // Save to file.
  6. std::ofstream fileSave;
  7. fileSave << docSave;

rapidxml使用的更多相关文章

  1. RapidXML 试用

    近半年来断断续续的封装一些SDK,在兼顾跨平台.易用性和高效率上还要顾及到对外dll的大小问题.由于之前解析SVG文件的用到了一个XML解析库xercesc,这个DLL实在巨大近4M,于是尝试用新的X ...

  2. C++中rapidxml用法及例子

    rapidxml是一个快速的xml库,比tinyxml快了50-100倍.本文给出创建.读取.写入xml的源码. 由于新浪博客不支持文本文件上传,在使用下面代码需要先下载 rapidxml,关于这个库 ...

  3. 警惕rapidxml的陷阱:添加节点时,请保证变量的生命周期

    http://www.cnblogs.com/chutianyao/p/3246592.html 项目中要使用xml打包.解析协议,HQ指定了使用rapidxml--号称是最快的xml解析器. 功能很 ...

  4. RAPIDXML 中文手册,根据官方文档完整翻译!

    简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...

  5. RapidXml用法

    一.写xml 文件 #include <iostream> #include "rapidxml/rapidxml.hpp" #include "rapidx ...

  6. [xml解析]rapidxml读取文件

    因为项目需要读取xml配置文件,在原来调查一番后,项目组使用了tinyxml. tinyxml确实简单,非常清楚的就把读取方案写出来了.但是,由于后期xml文件越来越大(2.5M,大概1w多行数据), ...

  7. rapidxml对unicode的支持

    为了提高duilib创建布局控件的效率,在LuaDui项目中使用rapidxml解析器替换了duilib库自带的xml解析器. duilib使用unicode编译,所以rapidxml需要解析unic ...

  8. [转]xml解析工具的效率比较QDomDocument、TinyXml-2、RapidXml、PugiXml

    转自:http://www.itdaan.com/blog/2017/02/20/301ad47832f4.html 由于windows环境下测试不稳定,博主选择在linux下进行的测试! Qt - ...

  9. XML文件生成C++代码(基于rapidxml)

    简述 与XML文件生成C++代码(基于pugixml)中的功能一致,只是这里改用的rapidxml来实现.就不多说了,直接放代码. 代码 #include "rapidxml-1.13/ra ...

随机推荐

  1. 51nod 1257 背包问题 V3

    1257 背包问题 V3 基准时间限制:3 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2.. ...

  2. Elasticsearch 5.0

    Elasticsearch 5.0 使用ES的基本都会使用过head,但是版本升级到5.0后,head插件就不好使了.下面就看看如何在5.0中启动Head插件吧! 官方粗略教程 Running wit ...

  3. MVC下用C#实现Excel导出

    Aspx页面脚本: function exportxls() { window.open("/Common/HomeExport?startdate=" + $("#hi ...

  4. 【读书笔记】Redis入门

    1:Redis概览 Remote Dictionary Server 远程字典服务 Redis是基于内存的存储 在一台普通的笔记本上,Redis每秒的读取速度可以达到10万 内存读取数据,断电的时候数 ...

  5. BZOJ 4031: [HEOI2015]小Z的房间 Matrix-Tree定理

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=4031 题解: Matrix-tree定理解决生成树计数问题,其中用到高斯消元法求上三角矩 ...

  6. oracle——外连接查询

    一.问题描述 有时我们为了保留某个表中的数据,而该表中的数据在另外一个关联表中未必都存在对应,此时就应该试用外连接查询. 比如:两个表,产品表和子产品表 注:子产品的parent_product_id ...

  7. winform窗口打开后文本框的默认焦点设置

    原文:http://blog.csdn.net/kongwei521/article/details/6871411 winform窗口打开后文本框的默认焦点设置,进入窗口后默认聚焦到某个文本框,两种 ...

  8. Binary Search Tree In-Order Traversal Iterative Solution

    Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...

  9. hdu 1524 A Chess Game 博弈论

    SG函数!! 代码如下: #include<stdio.h> #include<cstring> #define I(x) scanf("%d",& ...

  10. Thread的第三天学习

    线程互斥 public void method1() {  synchronized(this) {  ... } } 等同于 public synchronized void method1() { ...