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使用方法的更多相关文章
- javaSE27天复习总结
JAVA学习总结 2 第一天 2 1:计算机概述(了解) 2 (1)计算机 2 (2)计算机硬件 2 (3)计算机软件 2 (4)软件开发(理解) 2 (5) ...
- rapidxml读取包含中文路径的xml解析错误的解决方法
from http://blog.csdn.net/qinwei4072880/article/details/38865179 1.rapidxml不支持中文路径. 2.rapidxml不支持Uni ...
- RAPIDXML 中文手册,根据官方文档完整翻译!
简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...
- rapidxml使用
以前都是用tinyxml,这次开发中解析xml配置文件像尝试一下rapidxml,据说效率很高... RapidXml Manual: http://rapidxml.sourceforge.net/ ...
- RapidXml用法
一.写xml 文件 #include <iostream> #include "rapidxml/rapidxml.hpp" #include "rapidx ...
- [xml解析]rapidxml读取文件
因为项目需要读取xml配置文件,在原来调查一番后,项目组使用了tinyxml. tinyxml确实简单,非常清楚的就把读取方案写出来了.但是,由于后期xml文件越来越大(2.5M,大概1w多行数据), ...
- 利用c++操作XML,主要是内部循环方法的使用
本文主要分享的是循环方法的使用,设置XML节点属性,用了3种循环方法. XML文件: <?xml version='1.0' encoding='utf-8' ?><root> ...
- rapidxml 序列化
void TestRapidXml() { ]; sprintf(xmlContent,"<root><head>aaa</head><body&g ...
- mapreduce多文件输出的两方法
mapreduce多文件输出的两方法 package duogemap; import java.io.IOException; import org.apache.hadoop.conf ...
随机推荐
- MyEclipse部署项目报"Add Deployment". Invalid Subscription Level - Discontinuing this MyEclipse
"Add Deployment". Invalid Subscription Level - Discontinuing this MyEclipse 猜测应该是MyEclipse ...
- P2885
2885 code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; b ...
- request.getRemoteAddr() 获取的值为0:0:0:0:0:0:0:1的原因及解决办法
问题: 在近期开发的javaweb项目中,需要记录登录时的电脑ip地址和主机名,通过request.getRemoteAddr()和request.getRemoteHost()得到的值都是0:0:0 ...
- 探索sklearn | 鸢尾花数据集
1 鸢尾花数据集背景 鸢尾花数据集是原则20世纪30年代的经典数据集.它是用统计进行分类的鼻祖. sklearn包不仅囊括很多机器学习的算法,也自带了许多经典的数据集,鸢尾花数据集就是其中之一. 导入 ...
- [水煮 ASP.NET Web API2 方法论](1-7)CSRF-Cross-Site Request Forgery
问题 通过 CSRF(Cross-Site Request Forgery)防护,保护从 MVC 页面提交到ASP.NET Web API 的数据. 解决方案 ASP.NET 已经加入了 CSRF 防 ...
- ELK系列--实时日志分析系统ELK 部署与运行中的问题汇总
前记: 去年测试了ELK,今年测试了Storm,最终因为Storm需要过多开发介入而放弃,选择了ELK.感谢互联网上各路大神,目前总算是正常运行了. logstash+elasticsearch+ki ...
- python 网络爬虫框架scrapy使用说明
1 创建项目scrapy startproject tutorial 2 定义Itemimport scrapyclass DmozItem(scrapy.Item): title = scra ...
- Java 中自定义时间格式
DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date d = new Date(); String s ...
- 决策树:ID3与C4.5算法
1.基本概念 1)定义: 决策树是一个预测模型:他代表的是对象属性与对象值之间的一种映射关系,树中每个节点代表的某个可能的属性值. 2)表示方法: 通过把实例从根结点排列到某个叶子结点来分类实例,叶子 ...
- Django常用内置过滤器
1.add 此过滤器将首先尝试将两个值强制为整数.如果失败,它会尝试将值一起添加.这将工作在一些数据类型(字符串,列表等)和失败在其他人.如果失败,结果将是一个空字符串. {{ value | add ...