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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 零基础学Java(11)自定义类

    零基础学Java(11)自定义类前言之前的例子中,我们已经编写了一些简单的类。但是,那些类都只包含一个简单的main方法。现在来学习如何编写复杂应用程序所需要的那种主力类。通常这些类没有main方法,却有自己的实例字段和实例方法。

    2022年8月7日
    11
  • win10 常用命令「建议收藏」

    win10 常用命令「建议收藏」以下为windows10命令:1.calc:启动计算器2.appwiz.cpl:程序和功能3.certmgr.msc:证书管理实用程序4.charmap:启动字符映射表5.chkdsk.exe:Chkdsk磁盘检查(管理员身份运行命令提示符)6.cleanmgr:打开磁盘清理工具7.cliconfg:SQLSERVER客户端网络实用工具8.cmstp:连接管理器配置文件安装程…

    2022年5月30日
    161
  • django 渲染_django开源项目

    django 渲染_django开源项目前言渲染模块的原理和解析模块是一样,drf默认的渲染有2种方式,一种是json格式,另一种是模板方式。渲染模块源码入口入口:APIView类中dispatch方法中的:self.response

    2022年8月7日
    2
  • 第一章《初识数据库》

    第一章《初识数据库》

    2021年5月28日
    100
  • 赛罗终极形态_终极战斗仪

    赛罗终极形态_终极战斗仪实践是推广一个产品或技术的最好的方式,特别在IT行业让产品管理产品本事,就是一种很好的推广方式,例如DockerinDocker、k8sink8s,这些已经被大家普遍接受。所以我觉得spinnaker要想很好的推广开来,也需要类似的应用方案,我把它称为SpinnakerOnSpinnaker。架构图如下:最上面部分是主spinnaker,或者叫masterspinn…

    2025年7月22日
    5
  • vscode 快捷键绑定

    vscode 快捷键绑定最近迷上了vscode,用它开发.netcore程序十分方便,智能提示也很好用,插入智能提示的选项是enter键或者tab键,可惜我以前习惯使用vs写c#,习惯用空格做智能提示的选择,多方查找资料甚至准备采用开发一个vscode插件的方式解决,后来无意间查看官方文档,利用vscode的快捷键绑定功能是可以做到的。打开vscode,进入文件->首选项->键盘快捷方式查看’tab’的功能,其中就有一项:

    2022年5月18日
    43

发表回复

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

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