使用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


相关推荐

  • oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」

    oidc auth2.0_使用Spring Security 5.0和OIDC轻松构建身份验证「建议收藏」oidcauth2.0“我喜欢编写身份验证和授权代码。”〜从来没有Java开发人员。厌倦了一次又一次地建立相同的登录屏幕?尝试使用OktaAPI进行托管身份验证,授权和多因素身份验证。SpringSecurity不仅是一个功能强大且可高度自定义的身份验证和访问控制框架,它还是保护基于Spring的应用程序的实际标准。从前,SpringSecurity需要使用大量的XML来…

    2022年8月31日
    8
  • 模拟电子技术之运算放大器「建议收藏」

    模拟电子技术之运算放大器「建议收藏」上一篇文章对放大电路做了简单的介绍,相信大家对”放大”这个概念已经有了一定的了解,下面我们来看一下运算放大器运算放大器及其信号放大运算放大器的基本线性应用1.运算放大器及其信号放大集成运算放大器是一种应用极为广泛的模拟器件。用集成运算放大器可以非常方便地实现信号的放大、运算、变换等各种处理。常见的运放电路符号有矩形和三角形两种电路符号这里我们采用三角形符号端口意义运算放大器正常工作时,必须提供工作电源,通常正负电源的连接方式为:实际运放外部引脚实例来看一下实际的电子元器件:运

    2022年5月11日
    45
  • pycharm安装python库的步骤详细

    pycharm安装python库的步骤详细首先我们需要下载并安装好 pycharm 软件和 python 解释器 打开 pycharm 编译器 点击 file 点击深色部分的 settings 将光标放置到 Project 也就是深色区域单击左键 然后点击箭头所指的 projectinter 解释器 单机一下之后我们可以看到弹出下图所示的界面 他会显示出你当前已经安装的库 如果你想安装新的库可以点击箭头所指的 号点击 号之后

    2026年3月26日
    1
  • navicat15万能激活码(最新序列号破解)

    navicat15万能激活码(最新序列号破解),https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    586
  • StringBuilder的使用方法

    StringBuilder的使用方法1 StringBuilde 使用 StringBuilde newStringBui 2 长度的求取 sb length 3 StringBuilde 的添加和反转方法 publicString 任意类型 添加数据 并返回对象本身 eg sb append hello append world 链式调用 publicString 返回相反的字符序列 4 StringBu

    2026年3月26日
    3
  • 两个求和符号相乘_excel输入次方符号

    两个求和符号相乘_excel输入次方符号在机器学习中,经常会遇到有含有两个求和符号的公式,如,∑i=1M∑j=1N\sum^M_{i=1}\sum^N_{j=1}∑i=1M​∑j=1N​,一开始,我总是不能够理解这是一种怎样的运算,后来看到下面的解释觉得自己顿悟:有两个∑\sum∑的时候就有两个变量,是一个不变的情况下另一个从头到尾改变,然后之前那个再变一下,第二个再从头到尾变,一直到第一个变量变到最后,把这个过程中的项加起来!…

    2022年10月12日
    8

发表回复

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

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