rapidxml 文件读写、增加、删除、编辑节点

本文旨在提供RapidXml文件读写操作,以及对节点的增加、删除、编译提供一个Demo。测试的代码如下:

#include 
#include 
#include 
#include "rapidxml/rapidxml.hpp"
#include "rapidxml/rapidxml_utils.hpp"//rapidxml::file
#include "rapidxml/rapidxml_print.hpp"//rapidxml::print
using namespace rapidxml;

//
void WriteFile1()
{
	xml_document<> doc;
	xml_node<>* rot = doc.allocate_node(rapidxml::node_pi, doc.allocate_string("xml version='1.0' encoding='utf-8'"));
	doc.append_node(rot);

	xml_node<>* config = doc.allocate_node(node_element, "config","information");
	doc.append_node(config);

	xml_node<>* color = doc.allocate_node(node_element, "color",NULL);
	color->append_node(doc.allocate_node(node_element, "red", "88"));
	color->append_node(doc.allocate_node(node_element, "green", "108"));
	color->append_node(doc.allocate_node(node_element, "blue", "128"));
	color->append_node(doc.allocate_node(node_element, "alpha", "255"));
	config->append_node(color);

	xml_node<>* size = doc.allocate_node(node_element, "size", NULL);
	size->append_node(doc.allocate_node(node_element, "x", "1024"));
	size->append_node(doc.allocate_node(node_element, "y", "768"));
	config->append_node(size);

	xml_node<>* mode = doc.allocate_node(rapidxml::node_element, "mode", "screen mode");
	mode->append_attribute(doc.allocate_attribute("fullscreen", "false"));
	config->append_node(mode);

	//打印整个XML内容
	std::string text;
	rapidxml::print(std::back_inserter(text), doc, 0);
	std::cout << text << std::endl;

	//写入文件
	std::ofstream out("write_file1.xml");
	out << doc;
	out.close();

	/*
	
		
			88
			108
			128
			255
		
		
			1024
			768
		
		screen mode
	*/
}
//
void WriteFile2()
{
	xml_document<> doc;
	char strbuf[] = 
		/*""*/
		""
		""
		"LG"
		"Gigabyte"
		"Kingston"
		"MSI"
		"WD"
		""

		""
		"CSDN"
		"http://blog.csdn.net/hellokandy"
		"Guangzhou China"
		""
		"";
	doc.parse<0>( strbuf );

	//打印整个XML内容
	std::string text;
	rapidxml::print(std::back_inserter(text), doc, 0);
	std::cout << text << std::endl;

	//写入文件
	std::ofstream out("write_file2.xml");//ofstream默认时,如果文件存在则覆盖原来的内容,不存在则新建
	out << doc;
	out.close();
}
//
void ModifyFile()
{
	//
	file<> fdoc("write_file2.xml");
	xml_document<> doc;
	doc.parse<0>(fdoc.data());

	std::string text;
	rapidxml::print(std::back_inserter(text), doc, 0);//doc内容输出到text尾处
	std::cout << text << std::endl;

	xml_node<>* root = doc.first_node();
	xml_node<>* Devices = root->first_node();
	//xml_node<>* job = root->first_node("job");

	//移除根节点下的Devices结点下的MotherBoard节点
	xml_node<>* MotherBoard = Devices->first_node("MotherBoard");
	Devices->remove_node(MotherBoard);

	//
	text = "移除根节点下的Devices结点下的MotherBoard节点\r\n";
	rapidxml::print(std::back_inserter(text), doc, 0);
	std::cout << text << std::endl;

	//在Devices的Graphics节点处插入一个KeyBorad节点
	xml_node<>* Graphics = Devices->first_node("Graphics");//找到Graphics节点
	xml_node<>* new_node = doc.allocate_node(node_element, "KeyBorad", "Logitech");
	new_node->append_attribute(doc.allocate_attribute("Interface", "USB"));
	Devices->insert_node(Graphics, new_node);

	text = "在Devices下面插入一个KeyBorad节点\r\n";
	rapidxml::print(std::back_inserter(text), doc, 0);
	std::cout << text << std::endl;

	//移除根节点下的Devices结点(包括该结点下所有结点)
	root->remove_node(Devices);
	text = "移除根节点下的Devices结点(包括该结点下所有结点)\r\n";
	rapidxml::print(std::back_inserter(text), doc, 0);
	std::cout << text << std::endl;

	//移除根节点下所有结点
	root->remove_all_nodes();
	text = "移除根节点下所有结点\r\n";
	rapidxml::print(std::back_inserter(text), doc, 0);
	std::cout << text << std::endl;

	//写入文件
	std::ofstream out("write_file2_delete.xml");
	out << doc;
	out.close();
}
//
int _tmain(int argc, _TCHAR* argv[])
{
	//
	WriteFile1();
	
	//
	WriteFile2();

	//
	ModifyFile();

	system("pause");
	return 0;
}
值得一提的是:RapidXml是没有提供编辑节点的,但可以通过先删除节点,然后再插入的方式来解决

你可能感兴趣的:(HTML,/,XML)