dl
dl_file_xml.h
浏览该文件的文档.
1
12#pragma once
13#include <tinyxml2.h>
14
15namespace dl
16{
17using namespace tinyxml2;
18
19// 元素封装
21{
22public:
29 template<IsBase T>
30 void SetAttribute(std::string_view name, T value)
31 {
32 if (_invalid())
33 return;
34 _ele->SetAttribute(name.data(), value);
35 }
36 void SetAttribute(std::string_view name, std::string_view value)
37 {
38 if (_invalid())
39 return;
40 _ele->SetAttribute(name.data(), value.data());
41 }
42 void SetAttribute(std::string_view name, const std::string& value)
43 {
44 if (_invalid())
45 return;
46 _ele->SetAttribute(name.data(), value.c_str());
47 }
48
53 FileXmlElement GetChildElement(const std::vector<std::string_view>& vec_name)
54 {
56 if (vec_name.empty())
57 {
58 log_err("vec_name为空!");
59 return ret;
60 }
61
62 if (_invalid())
63 return ret;
64
65 ret._ele = _ele;
66 std::string str = ret._ele->Name();
67 for (const std::string_view& name : vec_name)
68 {
69 str += "->";
70 str += name;
71 ret._ele = ret._ele->FirstChildElement(name.data());
72 if (!ret._ele)
73 {
74 log_err("子元素获取失败:{}", str);
75 return ret;
76 }
77 }
78 return ret;
79 }
80 void Set(XMLElement* ele) { _ele = ele; }
81 XMLElement* Get() { return _ele; }
82
83
85 _ele{ nullptr }
86 {}
87private:
88 XMLElement* _ele;
89 bool _invalid()
90 {
91 if (!_ele)
92 {
93 log_err("ele无效!");
94 return true;
95 }
96 return false;
97 }
98};
99
101{
102public:
108 bool Open(std::string_view path_name)
109 {
110 if (_doc.LoadFile(path_name.data()) != XML_SUCCESS)
111 {
112 log_err("xml文件打开失败:{}", path_name);
113 return false;
114 }
115 return true;
116 }
117
122 bool Save(std::string_view path_name)
123 {
124 if (_doc.SaveFile(path_name.data()) != XML_SUCCESS)
125 {
126 log_err("xml文件保存失败:{}", path_name);
127 return false;
128 }
129 return true;
130 }
131
137 FileXmlElement GetChildElement(const std::vector<std::string_view>& vec_name)
138 {
139 FileXmlElement ret;
140 if (vec_name.empty())
141 {
142 log_err("vec_name为空!");
143 return ret;
144 }
145 XMLElement* ele = _doc.FirstChildElement(vec_name[0].data());
146 if (vec_name.size() == 1)
147 {
148 ret.Set(ele);
149 return ret;
150 }
151 ret.Set(_get_child_element(ele, vec_name));
152 return ret;
153 }
154private:
155 tinyxml2::XMLDocument _doc;
156
157 XMLElement* _get_child_element(XMLElement* ele, const std::vector<std::string_view>& vec_name)
158 {
159 if (!ele)
160 {
161 log_err("ele为空!");
162 return nullptr;
163 }
164 std::string str{ vec_name[0] };
165 for (size_t i = 1; i < vec_name.size(); ++i)
166 {
167 str += "->";
168 str += vec_name[i];
169 ele = ele->FirstChildElement(vec_name[i].data());
170 if (!ele)
171 {
172 log_err("子元素获取失败:{}", str);
173 return nullptr;
174 }
175 }
176 return ele;
177 }
178};
179
180
181}
FileXmlElement GetChildElement(const std::vector< std::string_view > &vec_name)
获取子元素
void Set(XMLElement *ele)
void SetAttribute(std::string_view name, T value)
设置属性
void SetAttribute(std::string_view name, const std::string &value)
void SetAttribute(std::string_view name, std::string_view value)
xml文件处理
FileXmlElement GetChildElement(const std::vector< std::string_view > &vec_name)
获取子元素
bool Save(std::string_view path_name)
保存到文件
bool Open(std::string_view path_name)
打开xml文件
#define log_err(...)