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


相关推荐

  • java定义byte类型,详解java中的byte类型[通俗易懂]

    java定义byte类型,详解java中的byte类型[通俗易懂]介绍byte,即字节,由8位的二进制组成。在Java中,byte类型的数据是8位带符号的二进制数。在计算机中,8位带符号二进制数的取值范围是[-128,127],所以在Java中,byte类型的取值范围也是[-128,127]。取值范围分析一直在想为什么不是-128到128呢?今天分析了一下这个问题。首先我们得明白一件事情,那就是运算规则:########################…

    2022年6月18日
    29
  • 计算机中二进制减法的问题是什么_二进制的减法运算例子

    计算机中二进制减法的问题是什么_二进制的减法运算例子有一道作业题,要求完成下列二进制数的减法运算:   00001100-11110111这道题分析说先把减数化成补码的形式,也就是要把11110111化成补码。如果把一个二进制数化成补码,先在最高位取1,再把各位取反加1。但是上面那个题它的第一位已经是1了,怎么化呀??悬赏分:0-解决时间:2010-3-1221:21;—————————–

    2022年9月24日
    2
  • 时间控件(选择时间范围的插件)「建议收藏」

    时间控件(选择时间范围的插件)「建议收藏」后台开发,一般都是有筛选条件的查询,那么问题就来了,根据日期范围搜索的情况下,插件要怎么选????Laydate时间控件这个是最开始,我采用的是两个时间插件,其他也没啥,就是运营部门使用起来可能感觉太麻烦,为啥不能一次让我选了,还有说老是忘记选择结束时间,然后就有了我接下来的工作。。。在此,给大家推荐一款很好使用的日期与时间组件…

    2022年5月10日
    37
  • 2020年到来,还不为来年的Python面试做准备?

    Python学习网有大量免费的Python入门教程,欢迎大家来学习。本文主要给大家介绍一些Python面试题,例如:迭代器和生成器的区别;什么是线程安全;什么是私有变量;内置变量;函数和方法;类;模块和包等等问题。

    2022年1月18日
    66
  • smtp服务器配置_smtp 服务器

    smtp服务器配置_smtp 服务器邮件首页SMTP地址SMTP登录用户名(例)是否验证端口SSL      mail.sohu.com smtp.sohu.com maoshen2010@sohu.com 是250mail.yeah.net smtp.yeah.net maoshen2010@yeah.net

    2022年10月4日
    2
  • getParameterValues 和 getParameter区别

    getParameterValues 和 getParameter区别一、getParameterValuesrequest.getParameterValues(Stringname)是获得如checkbox类(名字相同,但值有多个)的数据。接收数组变量,如checkobx类型二、getParameterrequest.getParameter(Stringname)是获得相应名的数据,如果有重复的名,则返回第一个的值….

    2022年7月22日
    23

发表回复

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

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