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

本文介绍如何利用TinyXML2库在C++环境中生成XML文件,并提供了添加、删除、查找及更新XML节点的具体实现。通过示例代码展示了如何创建XML结构,以及如何对XML节点进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

#include "tinyxml2.h"
using namespace tinyxml2;

把源码沾一下

#pragma once
#include "stdafx.h"
#include <string>
#include <vector>

using namespace std;
__declspec(dllexport)  void generate_XML(string path);
__declspec(dllexport) int add_XML(string path, string* addposion, int hierachy, string * attributes, int count_attributes,int flag);
__declspec(dllexport) int delete_XML(string path, string* deleteposion, int hierachy);
__declspec(dllexport) int find_XML(string path, string* findposion, int hierachy, vector<string> &str);
__declspec(dllexport) int updata_XML(string path, string* updataposion, int hierachy, string attributes);
#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<string> &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;
}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值