RapidXml使用

RapidXml使用vs2017rapidxml-1.131RapidXml使用1.1创建xml#include<iostream>#include”rapidxml/rapidxml.hpp”#include”rapidxml/rapidxml_utils.hpp”#include”rapidxml/rapidxml_print.hpp”usingnamespacerapidxml;voidcrateXml(){ xml_document<>doc; x

大家好,又见面了,我是你们的朋友全栈君。

vs2017
rapidxml-1.13

1 RapidXml使用

1.1 创建xml

#include <iostream>
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"
#include "rapidxml/rapidxml_print.hpp"

using namespace rapidxml;

void crateXml()
{ 
   
	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;
}

int main()
{ 
   
	crateXml();

	system("PAUSE");
	return EXIT_SUCCESS;
}

输出文件config.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>

1.2 创建xml2

void createXml2()
{ 
   
	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();//去顶级结点
	std::cout << (node->name()) << std::endl;
	node = node->first_node();
	while (node) { 
   
		std::cout << node->name() << ":" << node->value() << std::endl;//name() value()返回的字符串不会去掉首尾的空白字符
		node = node->next_sibling();
	}

	std::ofstream out("test.xml");//ofstream 默认时,假设文件存在则会覆盖原来的内容,不存在则会新建
	out << doc;//doc 这样输出时在目标文件里不会有xml 头信息---<?xml version='1.0' encoding='utf-8' >
	out.close();

}

输出文件test.xml:

<top>
	<name>tangqiang</name>
	<age>22</age>
</top>

1.3 读取xml

void readXml()
{ 
   
	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;

}

输出文件config.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>

1.4 删除节点

void removeNode()
{ 
   
	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;

}

输出文件test.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>


删除一个节点

<config>
    <size>
        <x>640</x>
        <y>480</y>
        <w>0</w>
        <h>0</h>
    </size>
    <mode fullscreen="false">screen mode</mode>
</config>


删除全部节点

<config/>

2 问题记录

2.1 错误 C3861 “print_children”: 找不到标识符

在这里插入图片描述
解决方法:
在print_node函数处,rapidxml_print.hpp 104行,前置声明调用到的函数

template<class OutIt, class Ch>
inline OutIt print_children(OutIt out, const xml_node<Ch>* node, int flags, int indent);

template<class OutIt, class Ch>
inline OutIt print_element_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

template<class OutIt, class Ch>
inline OutIt print_data_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

template<class OutIt, class Ch>
inline OutIt print_cdata_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

template<class OutIt, class Ch>
inline OutIt print_declaration_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

template<class OutIt, class Ch>
inline OutIt print_comment_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

template<class OutIt, class Ch>
inline OutIt print_doctype_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

template<class OutIt, class Ch>
inline OutIt print_pi_node(OutIt out, const xml_node<Ch>* node, int flags, int indent);

原文连接: https://www.cnblogs.com/rainbow70626/p/10386131.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160878.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 大数据_03【大数据基础知识】

    大数据_03【大数据基础知识】大数据_0301大数据概述02什么是大数据?(BigData)03传统数据与大数据的对比04大数据的特点4.1传统数据与大数据处理服务器系统安装对比4.2大数据下服务器系统安装![在这里插入图片描述](https://img-blog.csdnimg.cn/20201006090915426.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV

    2022年5月4日
    44
  • 在线词云制作生成 tagxedo

    在线词云制作生成 tagxedo在线词云制作生成tagxedo在线词云10行Python代码的词云待办在线词云原博文地址和详细使用方法介绍在线词云制作tagxedo在线网址http://www.tagxedo.com/注意点:直接访问http://www.tagxedo.com/app.html可能会超时,先进主页再点击右上角的create按钮进入创作页面会加载更快;需要下载一个软件,使用IE浏览器可加载上述软件,谷歌和EDGE实测都无法加载;导出的词云图片:10行Python代码的词云原博客地址:1

    2025年7月21日
    5
  • linux socket udp编程_linux网络编程socket

    linux socket udp编程_linux网络编程socket浅谈UDP。UDP是一种不可靠的、无连接的、数据报服务。发送端应用程序每执行一次写操作,UDP模块就将其封装成一个UDP数据报发送。接收端必须及时针对每一个UDP数据报执行读操作,否则就会丢包。并且,如果用户没有指定足够的应用程序缓冲区来读取UDP数据,则UDP数据将被截断。因此,采用UDP协议时必须要求接收端可以一次性收取完发送端一次发送的数据,不然就会造成数据丢失。2.客户端3.输出结果UDP是一种无连接的传输方式,所以可以多个客户端同时发送。服务器端关闭立马重启,客户

    2025年10月3日
    2
  • sqlserver 动态sql执行execute和sp_executesql

    sqlserver 动态sql执行execute和sp_executesqlsqlserver动态sql的执行,有两个方法execute和sp_executesql.其中第一个方法execute可以简写为exec.execute方法适合执行没有返回值的动态sql,sp_executesql可以获取到动态sql的返回值.二者比较起来,前者写起来简单,后者功能强大些,但写起来麻烦,使用的时候具体情况具体分析吧.  在function中不能使用exec和sp_exec

    2022年5月22日
    40
  • goland 激活码2021(破解版激活)

    goland 激活码2021(破解版激活),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    476
  • python hashlib_python中hashlib模块用法示例

    python hashlib_python中hashlib模块用法示例我们以前介绍过一篇 Python 加密的文章 Python 加密的实例详解 今天我们看看 python 中 hashlib 模块用法示例 具体如下 hashlibhashl 主要提供字符加密功能 将 md5 和 sha 模块整合到了一起 支持 md5 sha1 sha224 sha256 sha384 sha512 等算法具体应用 usr bin envpython coding UTF 8

    2025年10月29日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号