rapidxml的简单使用

4 篇文章 0 订阅
2 篇文章 0 订阅
Write
#include <iostream>
#include <string>
#include <fstream>
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
/*
xml
Documnet
Declaration---Node
Element---Node
Comment;    //注释  ---Node

Attribute;  //属性 
Text;       //内容
//层级收
rapidxml所表示的类型都为类模板
*/
static const int buf_len = 1024;
static char buf[buf_len] = { 0 };


void WriteXml(const char * file_name)
{
	rapidxml::xml_document<> doc;
	// 声明
	rapidxml::xml_node<>* declaration = doc.allocate_node(rapidxml::node_declaration);
	declaration->append_attribute(doc.allocate_attribute("version", "1.0"));
	declaration->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
	doc.append_node(declaration);//层级收

								 // 根节点
	rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "root");
	doc.append_node(root);

	// 注释节点1
	rapidxml::xml_node<>* comment1 = doc.allocate_node(rapidxml::node_comment, 0, "all students info");
	root->append_node(comment1);

	// 普通节点1
	rapidxml::xml_node<>* students = doc.allocate_node(rapidxml::node_element, "students");
	for (int i = 0; i < 10; ++i)
	{
		rapidxml::xml_node<>* one_student = doc.allocate_node(rapidxml::node_element, "student");
		sprintf_s(buf, "student_%02d", i);//format
		// doc.allocate_string 的作用是将源字符串深拷贝一份
		one_student->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf)));
		one_student->append_attribute(doc.allocate_attribute("score", doc.allocate_string(std::to_string(100 - i).c_str())));
		students->append_node(one_student);
	}
	root->append_node(students);


	// 注释节点2
	rapidxml::xml_node<>* comment2 = doc.allocate_node(rapidxml::node_comment, 0, "all books info");
	root->append_node(comment2);
	// 普通节点2
	rapidxml::xml_node<>* books = doc.allocate_node(rapidxml::node_element, "books");
	for (int i = 0; i < 10; ++i)
	{
		rapidxml::xml_node<>* one_book = doc.allocate_node(rapidxml::node_element, "book");
		sprintf_s(buf, "book_%02d", i);
		// doc.allocate_string 的作用是将源字符串深拷贝一份
		one_book->append_attribute(doc.allocate_attribute("name", doc.allocate_string(buf)));
		one_book->append_attribute(doc.allocate_attribute("price", doc.allocate_string(std::to_string(50 - i).c_str())));
		books->append_node(one_book);
	}
	root->append_node(books);


	char *end = rapidxml::print(buf, doc, 0);//将xml文档写入buff
	*end = 0;//加结束符
	std::cout << buf;


	std::ofstream outfile(file_name, std::ios::out);
	if (outfile)
	{
		outfile << buf;
		outfile.close();
	}

}

int main()
{
	WriteXml("info.xml");

	return 0;
}
Read
#include <iostream>
#include <fstream>
#include "rapidxml.hpp"

static const int buf_len = 1024;
static char buf[buf_len] = { 0 };
void ReadXml(const char * file_name)
{
	std::ifstream infile(file_name, std::ios::in);
	if (!infile)
	{
		return;
	}
	infile.read(buf, buf_len);
	std::cout << buf << std::endl;

	rapidxml::xml_document<> doc;//doc document
	doc.parse<0>(buf);//解析获取xml文档
	// 从xml中取得根节点
	rapidxml::xml_node<> *root = doc.first_node("root");
	// 遍历students的子节点
	for (rapidxml::xml_node<> * node = root->first_node("students")->first_node(); node; node = node->next_sibling())
	{
		std::cout << node->first_attribute("name")->value() << ", "
			<< node->first_attribute("score")->value() << std::endl;
	}
	// 遍历books的子节点
	for (rapidxml::xml_node<> * node = root->first_node("books")->first_node(); node; node = node->next_sibling())
	{
		std::cout << node->first_attribute("name")->value() << ", "
			<< node->first_attribute("price")->value() << std::endl;
	}
}

int main()
{
	ReadXml("info.xml");

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值