C++ XML的创建、读取和修改(一)

跟C#不太一样,需要调用一个TinyXML库来生成XML,库的源码地址为https://github.com/leethomason/tinyxml2,找到“clone or down”进行下载,,解压之后,将tinyxml2.h 和 tinyxml2.cpp放入工程文件中,调用方式为

#include "tinyxml2.h"
using namespace tinyxml2;

把源码沾一下

#include "ConfigureSetting.h"
#include "tinyxml2.h"

using namespace tinyxml2;

/*
创建,或者初始化xml
*/
void generate_XML(string path)
{
	XMLDocument doc;
	XMLDeclaration *dec = doc.NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\"");
	doc.InsertFirstChild(dec);

	//创建算法主节点
	XMLElement* ALgorithm = doc.NewElement("Algorithm");
	doc.InsertEndChild(ALgorithm);

	//创建算法分支点
	XMLElement* D1 = doc.NewElement("D1");
	ALgorithm->InsertEndChild(D1);

	//创建滤波算法分支点
	XMLElement* D1_filter = doc.NewElement("D1_filter");
	D1->InsertEndChild(D1_filter);

	//创建中值滤波算法分支点
	XMLElement* median_blur = doc.NewElement("median_blur");
	median_blur->SetAttribute("width","3");
	median_blur->SetAttribute("cut_boundary","3");
	D1_filter->InsertEndChild(median_blur);

	//创建均值滤波算法分支点
	XMLElement* aver_blur = doc.NewElement("aver_blur");
	aver_blur->SetAttribute("width","3");
	D1_filter->InsertEndChild(aver_blur);

	const char* path_save = path.c_str();
	doc.SaveFile(path_save,false);
}

/*
添加节点
addposion,从外到添加节点的路径
hierachy,节点的层数,包括子元素
attributes,添加元素
count_attributes,元素个数
flag,为1时,在子节点添加,为2时,在兄弟节点添加
*/
int add_XML(string path, string* addposion, int hierachy, string * attributes, int count_attributes,int flag)
{
	XMLDocument doc;
	const char* path_add = path.c_str();
	if (doc.LoadFile(path_add)) return 0;
	const char* first_posion = (addposion[0]).c_str();
	XMLElement *current_root = doc.FirstChildElement(first_posion);//翻查主节点
	for (int i = 0; i < hierachy - 1 - flag; i++)
	{
		const char* current_posion = addposion[i+1].c_str();
		current_root = current_root->FirstChildElement(current_posion);//翻查分节点
	}
	const char* final_posion = (addposion[hierachy - 1]).c_str();
	XMLElement *final_root = doc.NewElement(final_posion);
	for (int i = 0; i < count_attributes/2;i++)
	{
		const char* s1 = attributes[2 * i].c_str();
		const char* s2 = attributes[2*i+1].c_str();
		final_root->SetAttribute(s1,s2);
	}

	current_root->InsertEndChild(final_root);
	doc.SaveFile(path_add, false);
	return 1;
}

/*
删除给定节点
*/
int delete_XML(string path, string* addposion, int hierachy)
{
	XMLDocument doc;
	const char* path_delete = path.c_str();
	if (doc.LoadFile(path_delete)) return 0;
	const char* first_posion = (addposion[0]).c_str();
	XMLElement *current_root = doc.RootElement();
	for (int i = 0; i < hierachy-2;i++)
	{
		const char* current_posion = addposion[i+1].c_str();
		current_root = current_root->FirstChildElement(current_posion);
	}
	const char* final_posion = addposion[hierachy-1].c_str();
	XMLElement *finel_root = current_root->FirstChildElement(final_posion);
	current_root->DeleteChild(finel_root);

	doc.SaveFile(path_delete, false);
	return 1;
}

/*
查找节点的子节点
*/
int find_XML(string path, string* addposion, int hierachy, vector &vs)
{
	XMLDocument doc;
	const char* path_delete = path.c_str();
	if (doc.LoadFile(path_delete)) return 0;
	const char* first_posion = (addposion[0]).c_str();
	XMLElement *current_root = doc.RootElement();
	for (int i = 0; i < hierachy - 1; i++)
	{
		const char* current_posion = addposion[i + 1].c_str();
		current_root = current_root->FirstChildElement(current_posion);
	}
	const char* final_posion = addposion[hierachy - 1].c_str();
	XMLElement* final_root = current_root->FirstChildElement();
	if( final_root == NULL)  return 0;
	while (final_root!=NULL)
	{
		string name(final_root->Name());
		vs.push_back(name);
		final_root=final_root->NextSiblingElement();
	}
	return 1;
}

/*
修改节点属性
*/
int updata_XML(string path, string* updataposion, int hierachy, string attributes)
{
	XMLDocument doc;
	const char* path_updata = path.c_str();
	if (doc.LoadFile(path_updata)) return 0;
	const char* first_posion = (updataposion[0]).c_str();
	XMLElement *current_root = doc.RootElement();
	for (int i = 0; i < hierachy - 3; i++)
	{
		const char* current_posion = updataposion[i + 1].c_str();
		current_root = current_root->FirstChildElement(current_posion);
	}
	const char* final_posion = updataposion[hierachy - 2].c_str();
	XMLElement* final_root = current_root->FirstChildElement();
	if (final_root == NULL) return 0;
	final_root->SetAttribute(updataposion[hierachy -1].c_str(), attributes.c_str());
	doc.SaveFile(path_updata, false);
	return 1;
}

 

你可能感兴趣的:(C++,算法学习和研究)