跟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<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;
}