以前都是用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. 微软职位内部推荐-Senior Software Development En

    微软近期Open的职位: Job Title: Senior Development Engineer Division: Visual Studio China - Developer Divisi ...

  2. VS2013中自动缩进和注释的快捷键

    自动缩进: ctrl +k 再 ctrl +f 注释: ctrl+k 再 ctrl +c 取消注释: ctrl+k 再 ctrl+u

  3. cocos3.2版本中的一些新特性

    1.设置屏幕分辨率的大小,需要手动添加: 2.去掉了所有CC开头的命名: 3.所有的单例(以前是采用shared开头方法),全部改为getInstance(); 4.cocos3.x以上的版本支持C+ ...

  4. DVDRW光驱无法读DVD刻录盘

    原文地址:http://www.douban.com/note/206741292/ 所有的DVD RW驱动器,不能识别DVD ROM光盘问题,都是因为DVD区域没有给定造成的,除非设备损坏!在计算机 ...

  5. C# 获取数组的子集

    private static void PrintSubItems(int[] source) { int i = 1; int total = int.Parse(Math.Pow(2, sourc ...

  6. mysql存储过程出现OUT or INOUT argument 10 for routine

    OUT or INOUT argument 10 for routine * is not a variable or NEW pseudo-variable 我查网上很多出现在call的时候没有添加 ...

  7. GridView 服务端控件添加 js

    针对服务端控件的 CommandField “Delete” 添加 js $("#GridView1").find("a").each( function() ...

  8. C/C++ 快速排序 quickSort

    下面的动画展示了快速排序算法的工作原理. 快速排序图示:可以图中在每次的比较选取的key元素为序列最后的元素. #include <stdio.h> #include <stdlib ...

  9. What is Object Oriented Design? (OOD)

    Object Oriented Design is the concept that forces programmers to plan out their code in order to hav ...

  10. Sublime Text 3配置与vim模式(待完整)

    Sublime Text 3通过设置默认值与用户值的方式,来进行配置.默认值不允许更改,用户值是用户进行配置.同一属性,当用户值存在时,默认值就无效.打开Preference,如图: 先贴下我的Set ...