工具製作:xml处理工具

本来是想要实现config工具的,然而比较好用的配置文件的格式是xml,于是就先做一个xml的工具;
http://img2.58codes.com/2024/20139212EwDjT91LZk.png

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

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

http://img2.58codes.com/2024/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;}

效果:
http://img2.58codes.com/2024/20139212vvQQNlT5dJ.png


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章