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

相关推荐

  • PHP 正则表达式匹配函数 preg_match 与 preg_match_all

    PHP 正则表达式匹配函数 preg_match 与 preg_match_all

    2021年10月12日
  • Java知识体系最强总结(2021版)[通俗易懂]

    更新于2019-12-1510:38:00本人从事Java开发已多年,平时有记录问题解决方案和总结知识点的习惯,整理了一些有关Java的知识体系,这不是最终版,会不定期的更新。也算是记录自己在从事编程工作的成长足迹,通过博客可以促进博主与阅读者的共同进步,结交更多志同道合的朋友。特此分享给大家,本人见识有限,写的博客难免有错误或者疏忽的地方,还望各位大佬指点,在此表示感激不尽。文章目录…

    2022年4月6日
  • Mac OS 的一点历史: Mac OS, Mac OSX 与Darwin

    作为收购 NeXT 公司的结果,苹果公司获得了 NeXTSTEP 架构中的 Mach 和 Objective-C 等设计

    2021年12月26日
  • CAN协议学习(一)

    CAN协议学习(一)一、CAN协议的特点1)在总线空闲时,所有单元都可以发送消息,两个以上单元同时发送消息时,对各消息的Identifier进行逐位仲裁比较,仲裁获胜的单元(具有较高优先级)可继续发送消息,仲裁失败的单元停止发送。2)消息中没有地址,消息广播到总线上,任何总线上的单元都可以接收消息。在总线上增加单元,不会影响到其它的单元。3)最高数据传输速率1Mbps(距离小于40m),最远传输距离10k…

    2022年6月28日
  • 设计模式——门面模式「建议收藏」

    设计模式——门面模式「建议收藏」今天我们继续来学习前面没有学完的结构型设计模式中的一种:门面模式。门面模式也是一种不太常用的设计模式。所以,我们今天依旧是了解为主,暂时不去深入的学习。概述门面模式:(FacadeDesignPattern)门面模式也叫外观模式,门面模式为子系统提供一组统一的接口,定义一组高层接口让子系统更易用。门面模式原理和实现也比较简单,应用场景也比较明确,主要在接口设计方面使用。何时使用:解决易用性问题。解决性能问题。解决分布式事务问题。UML类图:角色组成:门面角色:客户端调用这个

    2022年11月2日
  • 手机号码归属地最新数据库2015年12月(附带采集更新程序)[通俗易懂]

    手机号码归属地最新数据库2015年12月(附带采集更新程序)[通俗易懂]手机号码归属地最新数据库2015年12月(附带采集更新程序)转:http://www.duanmu.org/log/mobile/昨天发现数据库的手机号归属地判断不准确,数据库该更新了,百度了下好像没有什么共享好的。以前是去淘宝上买的数据库,每次更新还得加钱,干脆自己做个算了,共享给大家。使用说明:1.单独号段的更新,请在手机号段里输入开始号

    2022年7月22日

发表回复

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

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