参考: 官网http://rapidxml.sourceforge.net/
https://blog.csdn.net/wqvbjhc/article/details/7662931
http://blog.sina.com.cn/s/blog_9b0604b40101o6fm.html

rapidxml_print.hpp修改代码:

#ifndef RAPIDXML_PRINT_HPP_INCLUDED#define RAPIDXML_PRINT_HPP_INCLUDED// Copyright (C) 2006, 2009 Marcin Kalicinski// Version 1.13// Revision $DateTime: 2009/05/13 01:46:17 $//! \file rapidxml_print.hpp This file contains rapidxml printer implementation#include "rapidxml.hpp"// Only include streams if not disabled#ifndef RAPIDXML_NO_STREAMS#include <ostream>#include <iterator>#endifnamespace rapidxml{///// Printing flagsconst int print_no_indenting = 0x1;   //!< Printer flag instructing the printer to suppress indenting of XML. See print() function.///// Internal//! \cond internalnamespace internal{///// Internal character operations// Copy characters from given range to given output iteratortemplate<class OutIt, class Ch>inline OutIt copy_chars(const Ch *begin, const Ch *end, OutIt out){while (begin != end)*out++ = *begin++;return out;}// Copy characters from given range to given output iterator and expand// characters into references (&lt; &gt; &apos; &quot; &amp;)template<class OutIt, class Ch>inline OutIt copy_and_expand_chars(const Ch *begin, const Ch *end, Ch noexpand, OutIt out){while (begin != end){if (*begin == noexpand){*out++ = *begin;    // No expansion, copy character}else{switch (*begin){case Ch('<'):*out++ = Ch('&'); *out++ = Ch('l'); *out++ = Ch('t'); *out++ = Ch(';');break;case Ch('>'): *out++ = Ch('&'); *out++ = Ch('g'); *out++ = Ch('t'); *out++ = Ch(';');break;case Ch('\''): *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('p'); *out++ = Ch('o'); *out++ = Ch('s'); *out++ = Ch(';');break;case Ch('"'): *out++ = Ch('&'); *out++ = Ch('q'); *out++ = Ch('u'); *out++ = Ch('o'); *out++ = Ch('t'); *out++ = Ch(';');break;case Ch('&'): *out++ = Ch('&'); *out++ = Ch('a'); *out++ = Ch('m'); *out++ = Ch('p'); *out++ = Ch(';'); break;default:*out++ = *begin;    // No expansion, copy character}}++begin;    // Step to next character}return out;}// Fill given output iterator with repetitions of the same charactertemplate<class OutIt, class Ch>inline OutIt fill_chars(OutIt out, int n, Ch ch){for (int i = 0; i < n; ++i)*out++ = ch;return out;}// Find charactertemplate<class Ch, Ch ch>inline bool find_char(const Ch *begin, const Ch *end){while (begin != end)if (*begin++ == ch)return true;return false;}///// Internal printing operationstemplate<class OutIt, class Ch>OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);    template<class OutIt, class Ch>OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);template<class OutIt, class Ch>OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);template<class OutIt, class Ch>OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);template<class OutIt, class Ch>OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);template<class OutIt, class Ch>OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);template<class OutIt, class Ch>OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);template<class OutIt, class Ch>OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent);// Print children of the node                               template<class OutIt, class Ch>inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent){for (xml_node<Ch> *child = node->first_node(); child; child = child->next_sibling())out = print_node(out, child, flags, indent);return out;}// Print nodetemplate<class OutIt, class Ch>inline OutIt print_node(OutIt out, const xml_node<Ch> *node, int flags, int indent){// Print proper node typeswitch (node->type()){// Documentcase node_document:out = print_children(out, node, flags, indent);break;// Elementcase node_element:out = print_element_node(out, node, flags, indent);break;// Datacase node_data:out = print_data_node(out, node, flags, indent);break;// CDATAcase node_cdata:out = print_cdata_node(out, node, flags, indent);break;// Declarationcase node_declaration:out = print_declaration_node(out, node, flags, indent);break;// Commentcase node_comment:out = print_comment_node(out, node, flags, indent);break;// Doctypecase node_doctype:out = print_doctype_node(out, node, flags, indent);break;// Picase node_pi:out = print_pi_node(out, node, flags, indent);break;// Unknowndefault:assert(0);break;}// If indenting not disabled, add line break after nodeif (!(flags & print_no_indenting))*out = Ch('\n'), ++out;// Return modified iteratorreturn out;}// Print attributes of the nodetemplate<class OutIt, class Ch>inline OutIt print_attributes(OutIt out, const xml_node<Ch> *node, int flags){for (xml_attribute<Ch> *attribute = node->first_attribute(); attribute; attribute = attribute->next_attribute()){if (attribute->name() && attribute->value()){// Print attribute name*out = Ch(' '), ++out;out = copy_chars(attribute->name(), attribute->name() + attribute->name_size(), out);*out = Ch('='), ++out;// Print attribute value using appropriate quote typeif (find_char<Ch, Ch('"')>(attribute->value(), attribute->value() + attribute->value_size())){*out = Ch('\''), ++out;out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('"'), out);*out = Ch('\''), ++out;}else{*out = Ch('"'), ++out;out = copy_and_expand_chars(attribute->value(), attribute->value() + attribute->value_size(), Ch('\''), out);*out = Ch('"'), ++out;}}}return out;}// Print data nodetemplate<class OutIt, class Ch>inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent){assert(node->type() == node_data);if (!(flags & print_no_indenting))out = fill_chars(out, indent, Ch('\t'));out = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);return out;}// Print data nodetemplate<class OutIt, class Ch>inline OutIt print_cdata_node(OutIt out, const xml_node<Ch> *node, int flags, int indent){assert(node->type() == node_cdata);if (!(flags & print_no_indenting))out = fill_chars(out, indent, Ch('\t'));*out = Ch('<'); ++out;*out = Ch('!'); ++out;*out = Ch('['); ++out;*out = Ch('C'); ++out;*out = Ch('D'); ++out;*out = Ch('A'); ++out;*out = Ch('T'); ++out;*out = Ch('A'); ++out;*out = Ch('['); ++out;out = copy_chars(node->value(), node->value() + node->value_size(), out);*out = Ch(']'); ++out;*out = Ch(']'); ++out;*out = Ch('>'); ++out;return out;}// Print element nodetemplate<class OutIt, class Ch>inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent){assert(node->type() == node_element);// Print element name and attributes, if anyif (!(flags & print_no_indenting))out = fill_chars(out, indent, Ch('\t'));*out = Ch('<'), ++out;out = copy_chars(node->name(), node->name() + node->name_size(), out);out = print_attributes(out, node, flags);// If node is childlessif (node->value_size() == 0 && !node->first_node()){// Print childless node tag ending*out = Ch('/'), ++out;*out = Ch('>'), ++out;}else{// Print normal node tag ending*out = Ch('>'), ++out;// Test if node contains a single data node only (and no other nodes)xml_node<Ch> *child = node->first_node();if (!child){// If node has no children, only print its value without indentingout = copy_and_expand_chars(node->value(), node->value() + node->value_size(), Ch(0), out);}else if (child->next_sibling() == 0 && child->type() == node_data){// If node has a sole data child, only print its value without indentingout = copy_and_expand_chars(child->value(), child->value() + child->value_size(), Ch(0), out);}else{// Print all children with full indentingif (!(flags & print_no_indenting))*out = Ch('\n'), ++out;out = print_children(out, node, flags, indent + 1);if (!(flags & print_no_indenting))out = fill_chars(out, indent, Ch('\t'));}// Print node end*out = Ch('<'), ++out;*out = Ch('/'), ++out;out = copy_chars(node->name(), node->name() + node->name_size(), out);*out = Ch('>'), ++out;}return out;}// Print declaration nodetemplate<class OutIt, class Ch>inline OutIt print_declaration_node(OutIt out, const xml_node<Ch> *node, int flags, int indent){// Print declaration startif (!(flags & print_no_indenting))out = fill_chars(out, indent, Ch('\t'));*out = Ch('<'), ++out;*out = Ch('?'), ++out;*out = Ch('x'), ++out;*out = Ch('m'), ++out;*out = Ch('l'), ++out;// Print attributesout = print_attributes(out, node, flags);// Print declaration end*out = Ch('?'), ++out;*out = Ch('>'), ++out;return out;}// Print comment nodetemplate<class OutIt, class Ch>inline OutIt print_comment_node(OutIt out, const xml_node<Ch> *node, int flags, int indent){assert(node->type() == node_comment);if (!(flags & print_no_indenting))out = fill_chars(out, indent, Ch('\t'));*out = Ch('<'), ++out;*out = Ch('!'), ++out;*out = Ch('-'), ++out;*out = Ch('-'), ++out;out = copy_chars(node->value(), node->value() + node->value_size(), out);*out = Ch('-'), ++out;*out = Ch('-'), ++out;*out = Ch('>'), ++out;return out;}// Print doctype nodetemplate<class OutIt, class Ch>inline OutIt print_doctype_node(OutIt out, const xml_node<Ch> *node, int flags, int indent){assert(node->type() == node_doctype);if (!(flags & print_no_indenting))out = fill_chars(out, indent, Ch('\t'));*out = Ch('<'), ++out;*out = Ch('!'), ++out;*out = Ch('D'), ++out;*out = Ch('O'), ++out;*out = Ch('C'), ++out;*out = Ch('T'), ++out;*out = Ch('Y'), ++out;*out = Ch('P'), ++out;*out = Ch('E'), ++out;*out = Ch(' '), ++out;out = copy_chars(node->value(), node->value() + node->value_size(), out);*out = Ch('>'), ++out;return out;}// Print pi nodetemplate<class OutIt, class Ch>inline OutIt print_pi_node(OutIt out, const xml_node<Ch> *node, int flags, int indent){assert(node->type() == node_pi);if (!(flags & print_no_indenting))out = fill_chars(out, indent, Ch('\t'));*out = Ch('<'), ++out;*out = Ch('?'), ++out;out = copy_chars(node->name(), node->name() + node->name_size(), out);*out = Ch(' '), ++out;out = copy_chars(node->value(), node->value() + node->value_size(), out);*out = Ch('?'), ++out;*out = Ch('>'), ++out;return out;}}//! \endcond///// Printing//! Prints XML to given output iterator.//! \param out Output iterator to print to.//! \param node Node to be printed. Pass xml_document to print entire document.//! \param flags Flags controlling how XML is printed.//! \return Output iterator pointing to position immediately after last character of printed text.template<class OutIt, class Ch> inline OutIt print(OutIt out, const xml_node<Ch> &node, int flags = 0){return internal::print_node(out, &node, flags, 0);}#ifndef RAPIDXML_NO_STREAMS//! Prints XML to given output stream.//! \param out Output stream to print to.//! \param node Node to be printed. Pass xml_document to print entire document.//! \param flags Flags controlling how XML is printed.//! \return Output stream.template<class Ch> inline std::basic_ostream<Ch> &print(std::basic_ostream<Ch> &out, const xml_node<Ch> &node, int flags = 0){print(std::ostream_iterator<Ch>(out), node, flags);return out;}//! Prints formatted XML to given output stream. Uses default printing flags. Use print() function to customize printing process.//! \param out Output stream to print to.//! \param node Node to be printed.//! \return Output stream.template<class Ch> inline std::basic_ostream<Ch> &operator <<(std::basic_ostream<Ch> &out, const xml_node<Ch> &node){return print(out, node);}#endif}#endif

main.cpp

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <memory>
#include <sstream>
using namespace std;//下面三个文件是本段代码需要的库文件
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"using namespace rapidxml;int CreateXml();
int ReadAndChangeXml();int main()
{CreateXml();ReadAndChangeXml();cout << "hello" << endl;return 0;
}
//创建一个名称为config.xml文件
int CreateXml()
{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;
}//读取并修改config.xml
int ReadAndChangeXml()
{file<> fdoc("config.xml");std::cout<<fdoc.data()<<std::endl;xml_document<>   doc;doc.parse<0>(fdoc.data());std::cout<<doc.name()<<std::endl;//! 获取根节点rapidxml::xml_node<>* root = doc.first_node();std::cout<<root->name()<<std::endl;//! 获取根节点第一个节点rapidxml::xml_node<>* node1 = root->first_node();std::cout<<node1->name()<<std::endl;rapidxml::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;}

编译:
g++ main.cpp -std=gnu++0x

转载于:https://www.cnblogs.com/shuqingstudy/p/11342991.html

rapidxml学习相关推荐

  1. 使用 rapidxml 做配置文件

    对于配置文件,一般会选用ini,xml 等等的配置格式.如何快速高效的从文件内读取自己想要的信息是每个做配置文件想要达到的效果.对以小型开发我们并不用时用到msxml这种重量级的解析器.那样会给自己添 ...

  2. 学习中碰到的一些优化工具包和库

    1.linear svm 这个工具包目前用的比较多.例如面部特征点的回归方法中,学习线性回归的权重,例如:Face Alignment at 3000 FPS中: minWt∑i=1N||△s^ti− ...

  3. 值得学习的C/C++开源框架(转)

    值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...

  4. rapidxml操作XML

    主要对上一篇文章做了修改,文章涉及创建.读取和修改XML文件,内容比较齐全,可以供大家学习. 创建xml文件: 基本步骤:给文件分配节点xmlDoc.allocate_node(node_elemen ...

  5. 在别的地方看的给程序员介绍一些C++开源库,记录给大家共同学习

    在别的地方看的<<给程序员介绍一些C++开源库>>,记录给大家共同学习 首先说明这篇文章不是出自我手,大家共同学习. 引用地址:http://oss.org.cn/?actio ...

  6. 值得学习的C语言开源项目

    值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...

  7. 值得学习的C++开源项目(转)

    值得学习的C语言开源项目 - 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的 ...

  8. C/C++开源项目资源,值得学习。

    C/C++开源项目资源~ 值得学习的C语言开源项目 - 1. Webbench Webbench是一个在Linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的UR ...

  9. 值得学习与推荐的c/c++框架和函数库

           这几天不上班,翻翻Evernote中记录的一些笔记,刚好有时间把记录的一些好玩链接转载一下.        这篇文章里提到的很多库都用过,尤其是图像处理相关库,尤其是opencv及cxi ...

  10. rapidjson安装学习

    这里主要记录几个要点,后面来补充吧,很晚了 源码是鹅厂大佬写的,佩服佩服~ 一.RapidJSON介绍及资料 RapidJSON是腾讯开源的C++ JSON解析及生成器,只有头文件的C++库,跨平台. ...

最新文章

  1. mysql1401错误_mysql错误代号-I(1401~1450)
  2. base64链接转为地址php,php将图片链接转换为base64编码文件流
  3. python:文件打包为exe
  4. 【最快人脸检测模型开源】libfacedetection开源
  5. 剑指offer python版 最长不含重复字符的子字符
  6. ubuntu下安装pythoncharm_Ubuntu下安装、激活并配置Pycharm
  7. 男子造出山寨ATM机 盗信息做假卡取钱13万
  8. ASCll码字符对照表
  9. HTTP协议解说以及TCP/IP认识
  10. android 有线网络,安卓手机免费“有线”上网
  11. 1.4.4 Performance Measures
  12. CodeForces 372A Counting Kangaroos is Fun 动物PK
  13. 学习篇之数据分析库pandas
  14. MySQL 测试数据(附开源网站)
  15. oracle创建dblink同义词,Oracle中DBlink与同义词
  16. C#获取http请求的JSON数据并解析
  17. android组合键截图原理,三星安卓手机怎么截图组合键 三星安卓手机截图组合键步骤...
  18. HTML生成word文档
  19. ​小城故事—逃离之路
  20. matplotlib绘制极坐标图 最全面总结

热门文章

  1. java junit测试类怎么写_15.junit测试类使用及注解
  2. 深入理解JVM - 虚拟机字节码指令集
  3. 基于C++和QT开发的校园超市库存物资管理系统
  4. PDF如何排版骑马钉打印
  5. ABSynthe : 侧信道攻击加密函数窃取密钥
  6. python实例 优化目标函数_python scipy optimize.minimize用法及代码示例
  7. 京东线报接口 全网一手线报全网(京东,淘宝,天猫)最全优惠信息
  8. 各类编程开发软件及资源全版本下载地址合集
  9. 华南理工大学 电力电子技术(王兆安) 期末复习笔记2 第三章第四章
  10. VGG16和VGG19网络结构图