RapidXml用法
一、写xml 文件
#include <iostream>
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp" using namespace rapidxml; int main()
{
xml_document<> doc;
xml_node<>* rot = doc.allocate_node(rapidxml::node_pi,doc.allocate_string("xml version='1.0' encoding='utf-8'"));
doc.append_node(rot);
xml_node<>* node = doc.allocate_node(node_element,"config","information");
xml_node<>* color = doc.allocate_node(node_element,"color",NULL);
doc.append_node(node);
node->append_node(color);
color->append_node(doc.allocate_node(node_element,"red","0.1"));
color->append_node(doc.allocate_node(node_element,"green","0.1"));
color->append_node(doc.allocate_node(node_element,"blue","0.1"));
color->append_node(doc.allocate_node(node_element,"alpha","1.0")); xml_node<>* size = doc.allocate_node(node_element,"size",NULL);
size->append_node(doc.allocate_node(node_element,"x","640"));
size->append_node(doc.allocate_node(node_element,"y","480"));
node->append_node(size); xml_node<>* mode = doc.allocate_node(rapidxml::node_element,"mode","screen mode");
mode->append_attribute(doc.allocate_attribute("fullscreen","false"));
node->append_node(mode); std::string text;
rapidxml::print(std::back_inserter(text), doc, 0); std::cout<<text<<std::endl; std::ofstream out("config.xml");
out << doc; system("PAUSE");
return EXIT_SUCCESS;
}
生成的xml例如以下
<?xml version="1.0" encoding="utf-8" ?>
- <config>
- <color>
<red>0.1</red>
<green>0.1</green>
<blue>0.1</blue>
<alpha>1.0</alpha>
</color>
- <size>
<x>640</x>
<y>480</y>
</size>
<mode fullscreen="false">screen mode</mode>
</config>
写文件样例2:
#include <string>
#include <iostream>
#include <fstream>
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp" using namespace rapidxml;
using namespace std; int main(int argc, char* argv[])
{ xml_document<> doc; //是解析器
char a[] = "<top>"//假设单独传, 就不能加上xml的头部信息,
//否则会报错
"<name>tangqiang</name>"
"<age>22</age>"
"</top>";
char* p = a;
doc.parse<0>(p); xml_node<>* node = doc.first_node();//去顶级结点
cout << (node->name())<< endl;
node = node->first_node();
while (node) {
cout << node->name() << node->value() << endl;//name() value()返回的字符串不会去掉首尾的空白字符
node = node->next_sibling();
} ofstream out("test.xml");//ofstream 默认时,假设文件存在则会覆盖原来的内容,不存在则会新建
out << doc;//doc 这样输出时在目标文件里不会有xml 头信息---<?xml version='1.0' encoding='utf-8' >
out.close();
system("pause");
return 0;
}
生成的xml例如以下
<top>
<name>tangqiang</name>
<age>22</age>
</top>
二、读取xml文件
#include <iostream>
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp" using namespace rapidxml; int main()
{
file<> fdoc("config.xml");
std::cout<<fdoc.data()<<std::endl;
xml_document<> doc;
doc.parse<0>(fdoc.data()); std::cout<<doc.name()<<std::endl; //! 获取根节点
xml_node<>* root = doc.first_node();
std::cout<<root->name()<<std::endl; //! 获取根节点第一个节点
xml_node<>* node1 = root->first_node();
std::cout<<node1->name()<<std::endl; xml_node<>* node11 = node1->first_node();
std::cout<<node11->name()<<std::endl;
std::cout<<node11->value()<<std::endl; //! 加入�之后再次保存
//须要说明的是rapidxml明显有一个bug
//那就是append_node(doc.allocate_node(node_element,"h","0"));的时候并不考虑该对象是否存在!
xml_node<>* size = root->first_node("size");
size->append_node(doc.allocate_node(node_element,"w","0"));
size->append_node(doc.allocate_node(node_element,"h","0")); std::string text;
rapidxml::print(std::back_inserter(text),doc,0); std::cout<<text<<std::endl; std::ofstream out("config.xml");
out << doc; system("PAUSE");
return EXIT_SUCCESS;
}
生成的xml为
<config>
<color>
<red>0.1</red>
<green>0.1</green>
<blue>0.1</blue>
<alpha>1.0</alpha>
</color>
<size>
<x>640</x>
<y>480</y>
<w>0</w>
<h>0</h>
</size>
<mode fullscreen="false">screen mode</mode>
</config>
三、删除节点
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp" #include<iostream>
using namespace rapidxml; int main()
{
file<> fdoc("config.xml");
xml_document<> doc;
doc.parse<0>(fdoc.data()); std::string text;
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout<<text<<std::endl; xml_node<>* root = doc.first_node(); xml_node<>* sec = root->first_node(); root->remove_node(sec); //移除根节点下的sec结点(包含该结点下全部结点)
text="删除一个节点\r\n";
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout<<text<<std::endl; root->remove_all_nodes(); //移除根节点下全部结点
text="删除全部节点\r\n";
rapidxml::print(std::back_inserter(text), doc, 0);
std::cout<<text<<std::endl; std::ofstream out("test.xml");
out<<doc;
system("pause");
return 0;
}
输出信息例如以下:
<config>
<color>
<red>0.1</red>
<green>0.1</green>
<blue>0.1</blue>
<alpha>1.0</alpha>
</color>
<size>
<x>640</x>
<y>480</y>
<w>0</w>
<h>0</h>
</size>
<mode fullscreen="false">screen mode</mode>
</config> 删除一个节点 <config>
<size>
<x>640</x>
<y>480</y>
<w>0</w>
<h>0</h>
</size>
<mode fullscreen="false">screen mode</mode>
</config> 删除全部节点 <config/>
四、编辑节点信息
临时找到的编辑方法就是先删除再添加�
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp" #include<iostream>
using namespace rapidxml; int main()
{
file<> fdoc("config.xml");
std::cout<<fdoc.data()<<std::endl;
xml_document<> doc;
doc.parse<0>(fdoc.data()); std::cout<<doc.name()<<std::endl; //! 获取根节点
xml_node<>* root = doc.first_node();
xml_node<>* delnode = root->first_node("color");
root->remove_node(delnode);//先删除address节点
//
xml_node<>* lnode = root->first_node("size");//找到post节点
xml_node<>* mynode=doc.allocate_node(node_element,"address","河北");
root->insert_node(lnode,mynode); std::string text;
rapidxml::print(std::back_inserter(text),doc,0); std::cout<<text<<std::endl; std::ofstream out("version.xml");
out << doc;
system("pause");
return 0;
}
输出例如以下:
<config> <color> <red>0.1</red> <green>0.1</green> <blue>0.1</blue> <alpha>1.0</alpha> </color> <size> <x>640</x> <y>480</y> <w>0</w> <h>0</h> </size> <mode fullscreen="false">screen mode</mode> </config> <config>
<address>河北</address>
<size>
<x>640</x>
<y>480</y>
<w>0</w>
<h>0</h>
</size>
<mode fullscreen="false">screen mode</mode>
</config>
五、遍历全部节点
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();
}
七、gcc使用-std=gnu++0x
编译rapidxml时会报错,错误信息大概例如以下
...rapidxml_print.hpp:120:23: error:
call to function 'print_element_node' thatis neither visible in the
template definition nor found byargument-dependent lookup
out = print_element_node(out, node, flags,indent);
^
...rapidxml_print.hpp:242:22: note:
'print_element_node' should be declaredprior to the call site or in
namespace 'rapidxml'
inline OutIt print_element_node(OutIt out,const xml_node<Ch> ...
经查,原来print_node()函数被其它函数调用,但在却没有定义(在被调用函数后定义了),所以解决方法为把print_node()函数移到print_children(), print_element_node() 等函数的后面。在原定义处就留一个函数声明即可。
详细diff文件例如以下。
Index: rapidxml_print.hpp
===================================================================
--- rapidxml_print.hpp (revision 2025)
+++ rapidxml_print.hpp (revision 2080)
@@ -101,68 +101,9 @@ ///////////////////////////////////////////////////////////////////////////
// Internal printing operations
-
- // Print node
+
template<class OutIt, class Ch>
- inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
- {
- // Print proper node type
- switch (node->type())
- {
-
- // Document
- case node_document:
- out = print_children(out, node, flags, indent);
- break;
-
- // Element
- case node_element:
- out = print_element_node(out, node, flags, indent);
- break;
-
- // Data
- case node_data:
- out = print_data_node(out, node, flags, indent);
- break;
-
- // CDATA
- case node_cdata:
- out = print_cdata_node(out, node, flags, indent);
- break;
-
- // Declaration
- case node_declaration:
- out = print_declaration_node(out, node, flags, indent);
- break;
-
- // Comment
- case node_comment:
- out = print_comment_node(out, node, flags, indent);
- break;
-
- // Doctype
- case node_doctype:
- out = print_doctype_node(out, node, flags, indent);
- break;
-
- // Pi
- case node_pi:
- out = print_pi_node(out, node, flags, indent);
- break;
-
- // Unknown
- default:
- assert(0);
- break;
- }
-
- // If indenting not disabled, add line break after node
- if (!(flags & print_no_indenting))
- *out = Ch('\n'), ++out;
-
- // Return modified iterator
- return out;
- }
+ inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent); // Print children of the node
template<class OutIt, class Ch>
@@ -372,7 +313,69 @@
*out = Ch('>'), ++out;
return out;
}
+
+ // Print node
+ template<class OutIt, class Ch>
+ inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
+ {
+ // Print proper node type
+ switch (node->type())
+ { + // Document
+ case node_document:
+ out = print_children(out, node, flags, indent);
+ break;
+
+ // Element
+ case node_element:
+ out = print_element_node(out, node, flags, indent);
+ break;
+
+ // Data
+ case node_data:
+ out = print_data_node(out, node, flags, indent);
+ break;
+
+ // CDATA
+ case node_cdata:
+ out = print_cdata_node(out, node, flags, indent);
+ break;
+
+ // Declaration
+ case node_declaration:
+ out = print_declaration_node(out, node, flags, indent);
+ break;
+
+ // Comment
+ case node_comment:
+ out = print_comment_node(out, node, flags, indent);
+ break;
+
+ // Doctype
+ case node_doctype:
+ out = print_doctype_node(out, node, flags, indent);
+ break;
+
+ // Pi
+ case node_pi:
+ out = print_pi_node(out, node, flags, indent);
+ break;
+
+ // Unknown
+ default:
+ assert(0);
+ break;
+ }
+
+ // If indenting not disabled, add line break after node
+ if (!(flags & print_no_indenting))
+ *out = Ch('\n'), ++out;
+
+ // Return modified iterator
+ return out;
+ }
+
}
//! \endcond
RapidXml用法的更多相关文章
- C++中rapidxml用法及例子
rapidxml是一个快速的xml库,比tinyxml快了50-100倍.本文给出创建.读取.写入xml的源码. 由于新浪博客不支持文本文件上传,在使用下面代码需要先下载 rapidxml,关于这个库 ...
- C++中rapidxml用法及例子(源码)
rapidxml是一个快速的xml库,比tinyxml快了50-100倍.本文给出创建.读取.写入xml的源码. 由于新浪博客不支持文本文件上传,在使用下面代码需要先下载 rapidxml, ...
- C++中rapidxml用法
转载:https://www.cnblogs.com/rainbow70626/p/7586713.html 解析xml是第三方库很多,例如:tingxml,这次学习一下rapidxml,rapidx ...
- 警惕rapidxml的陷阱:添加节点时,请保证变量的生命周期
http://www.cnblogs.com/chutianyao/p/3246592.html 项目中要使用xml打包.解析协议,HQ指定了使用rapidxml--号称是最快的xml解析器. 功能很 ...
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
随机推荐
- poj3468(线段树 边覆盖)
#include<cstdio> int lb,rb,data; long long sum[5000000],extra[5000000]; void add(int l,int r,i ...
- poj2391,poj2455
这两题本质是一致的: 一般来说,对于最长(短)化最短(长)的问题我们一般都使用二分答案+判定是否可行 因为这样的问题,我们一旦知道答案,就能知道全局信息 拿poj2455举例,对于二分出的一个答案,我 ...
- Oracle排序BUG
在今天项目开发中,遇到一个奇怪的问题,运用Oracle自身排序,然后将排序结果进行分页展示到前台时,发现数据有重复的现象. 这是数据表需要排序的全部结果,执行脚本:select * from ajb ...
- RMI、RPC、SOAP通信技术介绍及比对
1.RMI 使用java的程序员,对于RMI(RemoteMethod Invoke,远程方法调用)一定不陌生,在java中,为了在分布式应用开发时,能够方便调用远程对象,java提供了RMI的API ...
- ehcache 分布式集群同步数据实例
本文使用rmi方式,借鉴百度能搜到的文章,但是均不能做到数据同步,做了些改动完全没问题,更详细说明介绍百度即可.直奔主题,可运行的demo实例! 创建一个maven项目,配置pom pom.xml & ...
- 【转】Cocos2d-x 程序是如何开始运行与结束的
转自:http://blog.leafsoar.com/archives/2013/05-05.html 题记:对于技术,我们大可不必挖得那么深,但一定要具备可以挖得很深的能力 问题的由来 怎么样使用 ...
- (一)学习C#之浮点类型float小结
类型:float 大小:32位 范围a:±3.4E38 MSDNhttp://msdn.microsoft.com/zh-cn/library/b1e65aza.aspx 范围b: ±1.5E45~ ...
- [转载]DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
在网站设计的时候,应该注意css样式兼容不同浏览器问题,特别是对完全使用DIV CSS设计的网,就应该更注意IE6 IE7 FF对CSS样式的兼容,不然,你的网乱可能出去不想出现的效果! 所有浏览器 ...
- 将java应用程序打包成可执行文件
准备工作:我用的打包软件是installanywhere.因此要先下载这个软件,这里提供一个下载地址http://www.52z.com/soft/21747.html.(网页上写的有破解方法) 详细 ...
- mina2
远程通信 Mina2 学习笔记 作者:李少华 邮箱:xiaosanshaoli@126.com QQ:305409913 2010-12-23 初稿 引言... 1 一. Mina ...