使用rapidxml 生成xml文件[通俗易懂]

使用rapidxml 生成xml文件[通俗易懂]rapidxml是一个快速的xml库,有C++

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

      rapidxml是一个快速的xml库,由C++模板实现的高效率xml解析库,同时也是boost库的property_tree的内置解析库。

     当时rapidxml时,只需要把rapidxml.hpp 、 rapidxml_print.hpp 和 rapidxml_utils.hpp 三个文件拷贝到你的工程目录下,就可以了。

下面的是测试代码 main.cpp

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

using namespace std;

int main(int argc, char** argv) {
	vector<string>  v_str ;
	vector<string>::iterator it ;
	v_str.push_back("111111");
	v_str.push_back("222222");
	v_str.push_back("333333");
	v_str.push_back("444444");

	using namespace rapidxml;
	xml_document<> doc;  //构造一个空的xml文档
	xml_node<>* rot = doc.allocate_node(rapidxml::node_pi, doc.allocate_string("setting.xml version='1.0' encoding='utf-8'"));//allocate_node分配一个节点,该节点类型为node_pi,对XML文件进行描,描述内容在allocate_string中
	doc.append_node(rot); //把该节点添加到doc中

	xml_node<>* node = doc.allocate_node(node_element, "root", NULL);

	xml_node<>* analysis = doc.allocate_node(node_element, "Analysis", NULL);
	node->append_node(analysis);
	for (it = v_str.begin(); it != v_str.end(); it++) {
		xml_node<>* soinfo = doc.allocate_node(node_element, "soinfo", NULL);
		soinfo->append_attribute(doc.allocate_attribute("key", it->c_str()));
		analysis->append_node(soinfo);
	}

	xml_node<>* Output = doc.allocate_node(node_element, "Output", NULL);
	node->append_node(Output);

	xml_node<>* outinfo = doc.allocate_node(node_element, "outinfo", NULL);
	Output->append_node(outinfo);
	for (int j =0;j < 2; j++) {
		xml_node<>* type = doc.allocate_node(node_element, "desc", NULL); //分配一个type节点,
		type->append_attribute(doc.allocate_attribute("path", "123"));
		type->append_attribute(doc.allocate_attribute("relation", "345"));
		type->append_attribute(doc.allocate_attribute("priority", "567"));
		outinfo->append_node(type); //把type节点添加到节点outinfo中
	}

	for (it = v_str.begin(); it != v_str.end(); it++) {
		xml_node<>* rule = doc.allocate_node(node_element, "rule", NULL);
		Output->append_node(rule);
		for (int i = 0; i < 2 ; i++) {
			xml_node<>* cond = doc.allocate_node(node_element, "cond", NULL);
			cond->append_attribute(doc.allocate_attribute("key", "123"));
			cond->append_attribute(doc.allocate_attribute("value", "345"));
			cond->append_attribute(doc.allocate_attribute("relation","567"));
			rule->append_node(cond);
		}
		xml_node<>* out = doc.allocate_node(node_element, "out", NULL);
		out->append_attribute(doc.allocate_attribute("where", it->c_str()));
		rule->append_node(out);
	}

	doc.append_node(node);
	std::ofstream pout("config.xml");
	pout << doc;
	return 0;
}

下面是生成的xml文件 config.xml

<?setting.xml version='1.0' encoding='utf-8' ?>
<root>
	<Analysis>
		<soinfo key="111111"/>
		<soinfo key="222222"/>
		<soinfo key="333333"/>
		<soinfo key="444444"/>
	</Analysis>
	<Output>
		<outinfo>
			<desc path="123" relation="345" priority="567"/>
			<desc path="123" relation="345" priority="567"/>
		</outinfo>
		<rule>
			<cond key="123" value="345" relation="567"/>
			<cond key="123" value="345" relation="567"/>
			<out where="111111"/>
		</rule>
		<rule>
			<cond key="123" value="345" relation="567"/>
			<cond key="123" value="345" relation="567"/>
			<out where="222222"/>
		</rule>
		<rule>
			<cond key="123" value="345" relation="567"/>
			<cond key="123" value="345" relation="567"/>
			<out where="333333"/>
		</rule>
		<rule>
			<cond key="123" value="345" relation="567"/>
			<cond key="123" value="345" relation="567"/>
			<out where="444444"/>
		</rule>
	</Output>
</root>

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

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

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


相关推荐

  • Dreamweaver CS6安装教程

    Dreamweaver CS6安装教程一.解压二.安装点击忽略

    2022年6月9日
    39
  • linux arpspoof命令,Arpspoof的具体使用

    linux arpspoof命令,Arpspoof的具体使用比如我们要改百度的页面,哪么我们怎么做呢我们要先PING出百度的IP地址,然后在这个IP附近找同一个网段的服务器挨个入侵,就是通常说的C段,就会和百度在同一个交换机下了,然后使用我们提供的软件进行页面更改。看我的命令:arpspoof/i列出网卡数,记下序号,arpspoof/n生成一个JOB.txt这是规则文本,你可以使用TYPE来查看,并在里面加上HACKERBYsh3llc0d…

    2022年10月7日
    0
  • webgame开发中的文件解密

    webgame开发中的文件解密上一篇讲的是文件的加密,这一篇记录一下如何加载并显示图片。 因为图片文件经过加密,已经成了二进制流的文件了(一堆乱码),只能使用URLStream来加载资源了。这里有一点要注意:URLStream中的读取操作是非阻塞的,在读取数据之前必须使用bytesAvailable属性来确定是否能够获取足够的数据,如何不能获取足够的数据,将引发EOFError异常。 主要思路:1、加载…

    2022年6月7日
    28
  • 服务器的cd驱动器怎么修改盘符,windows 2016 修改驱动器盘符

    服务器的cd驱动器怎么修改盘符,windows 2016 修改驱动器盘符windows2016修改驱动器盘符内容精选换一换本文以服务器的操作系统为“WindowsServer2016Standard64bit”为例,提供云硬盘的初始化操作指导。MBR格式分区支持的磁盘最大容量为2TB,GPT分区表最大支持的磁盘容量为18EB,因此当为容量大于2TB的磁盘分区时,请采用GPT分区方式。关于磁盘分区形式的更多介绍,请参见初始化数据盘场景及磁盘分区形本文…

    2022年5月18日
    57
  • excel怎么只打印某页?excel怎么只打印某几页

    excel怎么只打印某页?excel怎么只打印某几页

    2021年11月9日
    164
  • 如何安装HAXM,如何解决HAXM installation failed问题

    如何安装HAXM,如何解决HAXM installation failed问题如何安装HAXM,如何解决HAXMinstallationfailed问题在安卓开发的过程中,大家都会需要用到安卓虚拟设备,在AndroidStudio中运行虚拟设备要求安装Intel硬件加速器(HAXM),虽然说是推荐使用但实际上不安装就无法运行虚拟设备。按照AndroidStudio提示的步骤安装HAXM,大多数人都会遇到HAXMinstallationfailed.ToinstallHAXMfollowtheinstructionsfoundat:https://so

    2022年6月28日
    24

发表回复

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

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