rapidxml学习记录
一、资料
官网:http://rapidxml.sourceforge.net/
参考:
https://blog.csdn.net/wqvbjhc/article/details/7662931
http://blog.sina.com.cn/s/blog_9b0604b40101o6fm.html
二、需要修改代码
rapidxml_print.hpp在// Internal printing operations
后添加声明
template<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);
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();
void DeleteNode();
void EditNode();
void PrintAllNode();
int main()
{CreateXml();//ReadAndChangeXml();//DeleteNode();//EditNode();PrintAllNode();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;//! 获取根节点第一个节点(没有利用first查找功能)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;//! 在size结点下添加(利用first查找功能)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;}void DeleteNode()
{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; }void EditNode()
{file<> fdoc("config.xml");xml_document<> doc;doc.parse<0>(fdoc.data());xml_node<>* root = doc.first_node();xml_node<>* delnode = root->first_node("color");root->remove_node(delnode);//先删除color节点xml_node<>* lnode = root->first_node("size");//找到size节点xml_node<>* mynode=doc.allocate_node(node_element,"address","河北");root->insert_node(lnode,mynode);//添加address兄弟节点std::string text; rapidxml::print(std::back_inserter(text), doc, 0); std::cout<<text<<std::endl; }void PrintAllNode()
{file<> fdoc("config.xml");xml_document<> doc;doc.parse<0>(fdoc.data());xml_node<>* root = doc.first_node();//依次找到兄弟结点,若兄弟结点仍有子节点,则无法打印值,需递归for(rapidxml::xml_node<char> * node = root->first_node("color");node != NULL;node = node->next_sibling()){std::cout<<node->name() << ": " << node->value()<<std::endl;}}
原型输出:
<?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>
rapidxml学习记录相关推荐
- Pytorch学习记录-torchtext和Pytorch的实例( 使用神经网络训练Seq2Seq代码)
Pytorch学习记录-torchtext和Pytorch的实例1 0. PyTorch Seq2Seq项目介绍 1. 使用神经网络训练Seq2Seq 1.1 简介,对论文中公式的解读 1.2 数据预 ...
- HTML5与CSS3权威指南之CSS3学习记录
title: HTML5与CSS3权威指南之CSS3学习记录 toc: true date: 2018-10-14 00:06:09 学习资料--<HTML5与CSS3权威指南>(第3版) ...
- springboot @cacheable不起作用_Springboot学习记录13 使用缓存:整合redis
本学习记录的代码,部分参考自gitee码云的如下工程.这个工程有详尽的Spingboot1.x教程.鸣谢! https://gitee.com/didispace/SpringBoot-Learnin ...
- 【Cmake】Cmake学习记录
Cmake学习记录 1.1 常例 add_library(gen_reference_infogen_reference_info/gen_reference_info.hgen_reference_ ...
- ASP.NETCore学习记录(一)
ASP.NETCore学习记录(一) asp.net core介绍 Startup.cs ConfigureServices Configure 0. ASP.NETCore 介绍 ASP.N ...
- Android开发技术周报176学习记录
Android开发技术周报176学习记录 教程 当 OkHttp 遇上 Http 2.0 http://fucknmb.com/2018/04/16/%E5%BD%93OkHttp%E9%81%87% ...
- add函数 pytorch_Pytorch学习记录-Pytorch可视化使用tensorboardX
Pytorch学习记录-Pytorch可视化使用tensorboardX 在很早很早以前(至少一个半月),我做过几节关于tensorboard的学习记录. https://www.jianshu.co ...
- java之字符串学习记录
java之字符串学习记录 public class StringDemo { public static void main(String[] args) { //静态初始化字符串 String s1 ...
- Redis的学习记录
Redis的学习记录 1.先导了解 1.1 NOSQL概述 1.1.1 为什么要用NoSql? 1.1.2 NoSql了解 1.1.3 NoSql特点 1.1.4 NoSQL的四大分类 2. Redi ...
- Django学习记录-1
2019独角兽企业重金招聘Python工程师标准>>> 作为Django的初学者,记录下我的学习历程,加深一下记忆(工作中用java,所以很容易忘记),如果有写的不对的地方,还望大家 ...
最新文章
- JavaScript Array相关方法
- 【干货】前端自学之路(持续更新)
- transforms函数查询
- Animatable API介绍
- java的输出的例子_Java例子:万年历的输出
- SAP CRM Genil Text-for-Key-Codes vs SAP C4C只读字段
- PHP学习笔记八【数组】
- java中项目启动时加载_如何在项目启动时,加载或解析某配置文件
- Redis 分布式锁遇到的序列化问题
- Error:File read error (source insight 4.0错误)
- html中取消li的点击事件,jquery设置html li点击click事件为什么无法赋值到表单input value中呢?...
- python中的reindex_Python学习笔记(6):Pandas的reindex方法
- oracle数据库图书,基于oracle数据库,创建图书表(一)
- mysql一秒查询次数_单个select语句实现MySQL查询统计次数
- php 预处理 防注入,PHP防止sql注入小技巧之sql预处理原理与实现方法分析
- 数字信号处理基础总结--7.28
- 背景色渐变html代码,求html文字背景色渐变的代码
- 使用 Arduino 烧录全新的 ATmega328P
- window窗口切换快捷键
- 计算机硬盘检测不到,硬盘检测不到怎么解决