使用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)
上一篇 2022年7月17日 上午7:36
下一篇 2022年7月17日 上午7:46


相关推荐

  • C++ strlen和sizeof区别

    C++ strlen和sizeof区别sizeof 是运算符 并不是函数 结果在编译时得到而非运行中获得 strlen 是字符处理的库函数 sizeof 参数可以是任何数据的类型或者数据 sizeof 参数不退化 strlen 的参数只能是字符指针且结尾是 0 的字符串 因为 sizeof 值在编译时确定 所以不能用来得到动态分配 运行时分配 存储空间的大小 constchar str name sizeof str 取的是指针 str 的长度 是 8strlen str

    2026年3月19日
    2
  • rose ha 配置

    rose ha 配置一 安装前准备工作 A 确认两台主机 磁盘阵列的型号 操作系统的版本 应用程序的类型和版本等信息 如果有卷管理软件 确认卷管理软件的类型和版本信息 确认两台主机网卡 网口 的数量 类型 RS232 串口的数量

    2026年3月26日
    1
  • Ubuntu 安装 gcc-4.9.3-64-gnu

    Ubuntu 安装 gcc-4.9.3-64-gnu可能每个人的环境不一样,所以安装的方法有些许差别。我参考了多个网络上的教程,在自己的ubuntu虚拟机中安装了gcc-4.9.3-64-gnu,记录一下自己的安装过程。虚拟机中默认安装了gcc-5.4.0,我要安装gcc-4.9.3一、下载地址:wgethttp://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-4.9.3/gcc-4.9.3.tar.bz2由于是用的虚拟机,配置不是很高,自己是windows下载完毕拷贝进虚拟机中。gcc-gn

    2022年7月24日
    17
  • 2021 goland激活破解方法「建议收藏」

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

    2022年3月14日
    660
  • matlab如何生成两个随机矩阵,matlab怎么生成随机矩阵

    matlab如何生成两个随机矩阵,matlab怎么生成随机矩阵1 rand 产生均值为 0 5 幅度在 0 1 之间的伪随机数 2 randn 产生均值为 0 方差为 1 的高斯白噪声 3 randperm n 产生 1 到 n 的均匀分布随机序列 4 normrnd a b c d 产生均值为 a 方差为 b 大小为 cXd 的随机矩阵在 MATLAB 中 我们常常会用到一些随机数据 那么这些数据是怎么生成的呢 下面一起来看一看方法 unifrnd a b 产生一个

    2026年3月19日
    2
  • 【Openclaw】安装部署Openclaw,并连接到飞书

    【Openclaw】安装部署Openclaw,并连接到飞书

    2026年3月13日
    3

发表回复

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

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