工具制作:xml处理工具

本来是想要实现config工具的,然而比较好用的配置文件的格式是xml,於是就先做一个xml的工具;
https://ithelp.ithome.com.tw/upload/images/20211217/20139212EwDjT91LZk.png

Xml作爲config 的好处是,可以用一个config配置多个config,比如我要都的config文件可以是多个,而且可以通过简单修改“target”实现读取不同的config;并且在子目录中,可以任意新增config档案,只需要继续新增处理这个config的功能就好;
https://ithelp.ithome.com.tw/upload/images/20211217/201392128ajkehSAoq.png

先来做一个xml的工具,结构图是这样:
目前实作的是xmlMaker
参数设计:
xmlMaker的参数:xmlpath,内容
数据结构设计:
层数,每层的名称和属性;属性是一个key-value对,
每层:名称,属性(key,value)*n;

https://ithelp.ithome.com.tw/upload/images/20211217/20139212fIu2ndZqbc.png

使用了c++的开源库:tinyxml2
实现的部分:

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

namespace Pz_XmlManagement
{
	 
	struct MyXml_Attribute
	{
		std::string key;
		std::string value;
	};
	struct MyXml_Element
	{
		std::string name;
		std::vector< MyXml_Attribute >attribute;
		std::vector< MyXml_Element > sub_elements;
	};

	struct MyXml_Xml
	{
		std::string root_name;
		std::vector< MyXml_Attribute > root_attribute;
		std::vector< MyXml_Element > root_elements;
	};

	class XmlMaker
	{
		public:
		void MakeXml(std::string xmlPath, MyXml_Xml xml_doc);
		void add_subElements(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* cur_element,std::vector< MyXml_Element > sub_elements);
	};

};

.cpp
#include "XmlMaker.h"
#include<queue>
using namespace tinyxml2;
void Pz_XmlManagement::XmlMaker::MakeXml(std::string xmlPath, MyXml_Xml xml_doc)
{
    XMLDocument doc;
    doc.InsertFirstChild(doc.NewDeclaration());
    //set root name and attributes
    XMLElement* root = doc.NewElement(xml_doc.root_name.c_str());
    for (int i = 0; i < xml_doc.root_attribute.size(); i++)
    {
        root->SetAttribute(xml_doc.root_attribute[i].key.c_str(), xml_doc.root_attribute[i].value.c_str());
    }
    doc.InsertEndChild(root);
    //add elements by layers
    add_subElements(&doc,root, xml_doc.root_elements);   

    doc.SaveFile(xmlPath.c_str());
}

void Pz_XmlManagement::XmlMaker::add_subElements(tinyxml2::XMLDocument* doc,tinyxml2::XMLElement* cur_element,std::vector< MyXml_Element > sub_elements)
{
    for (int i_ele = 0; i_ele < sub_elements.size(); i_ele++)
    {
        //set name
        XMLElement* element = doc->NewElement(sub_elements[i_ele].name.c_str());
        //set attribute
        for (int i_attri = 0; i_attri < sub_elements[i_ele].attribute.size(); i_attri++)
        {
            element->SetAttribute(sub_elements[i_ele].attribute[i_attri].key.c_str(), sub_elements[i_ele].attribute[i_attri].value.c_str());
        }
        cur_element->InsertEndChild(element);
        //add sub elements
        add_subElements(doc,element, sub_elements[i_ele].sub_elements);
    }
}

测试:

#include"XmlMaker.h"

int main()
{
    Pz_XmlManagement::XmlMaker* xmlmaker = new Pz_XmlManagement::XmlMaker();
    std::string xmlPath = "./xmlfiles/setup.xml";
    Pz_XmlManagement::MyXml_Xml myxml_doc;
    //define name
    myxml_doc.root_name = "Root";
    //define attributes
    Pz_XmlManagement::MyXml_Attribute tmp_attri;
    tmp_attri.key = "Target";
    tmp_attri.value = "Map";
    myxml_doc.root_attribute.push_back(tmp_attri);
    //define elements
    Pz_XmlManagement::MyXml_Element tmp_ele;
    tmp_ele.name = "Map";
    //sub elements
    Pz_XmlManagement::MyXml_Element tmp_sub_ele;
    tmp_sub_ele.name = "ServerSetup";
    tmp_ele.sub_elements.push_back(tmp_sub_ele);

    myxml_doc.root_elements.push_back(tmp_ele);
    
    xmlmaker->MakeXml(xmlPath,myxml_doc);
    return 0;
}

效果:
https://ithelp.ithome.com.tw/upload/images/20211217/20139212vvQQNlT5dJ.png


<<:  联储局加息步伐明朗化 炒股想胜算高最好买………

>>:  为了转生而点技能-JavaScript,day22(Arrow Function介绍

[Day_17]回圈与生成式 - (3)

巢状回圈 巢状回圈并非新的程序结构,只是回圈范围内又有回圈,巢状回圈可以有好几层,巢状回圈与单层回圈...

[day-17] 认识Python的资料结构!(Part .4)

一、认识"set"(集合)   甚麽是set呢?简单来说set就像是一个大杂烩,...

Day22 AWS - SignUp & MFA

先前开发的功能都是只有在本机执行,顶多同一个区域网路的用户可以使用服务,还是得想个办法让其他外网用户...

{DAY 29} Seaborn

前言 Seaborn是比matplotlib功能更强大的绘图套件 是建立在matplotlib的基础...

用 Python 畅玩 Line bot - 02:Line bot SDK

在建立好帐号之後,我们可以开始来看看 Line bot SDK,可以从 Line developer...