ver:一个可用版本,基本包含导入导出数据,备份,替换,搜索等功能。
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
#include "xml_opr.h"
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
CXmlOpr::CXmlOpr() = default;
|
||||
CXmlOpr::~CXmlOpr() = default;
|
||||
@@ -12,11 +15,35 @@ bool CXmlOpr::open(const std::string& xml_path)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CXmlOpr::backup_file(const std::string& desti_folder, const std::string& time)
|
||||
{
|
||||
if (!fs::exists(xml_path_)) {
|
||||
return false;
|
||||
}
|
||||
if (!fs::exists(desti_folder)) {
|
||||
fs::create_directories(desti_folder);
|
||||
}
|
||||
fs::path file_path = fs::path(xml_path_);
|
||||
fs::path des = fs::path(desti_folder).append(file_path.stem().string() + "_" + time + ".xml");
|
||||
return fs::copy_file(xml_path_, des);
|
||||
}
|
||||
|
||||
void CXmlOpr::set_baseinfo(const OprBase& base)
|
||||
{
|
||||
opr_base_ = base;
|
||||
}
|
||||
|
||||
bool CXmlOpr::get_all_elements(std::vector<Element_t*>& vec)
|
||||
{
|
||||
vec.clear();
|
||||
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
|
||||
while (purpose_node) {
|
||||
vec.push_back(purpose_node);
|
||||
purpose_node = purpose_node->NextSiblingElement();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
|
||||
{
|
||||
std::string next_node{};
|
||||
@@ -43,12 +70,7 @@ bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
|
||||
parent_node_ = parent_node_->FirstChildElement(item.c_str());
|
||||
}
|
||||
}
|
||||
vec.clear();
|
||||
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
|
||||
while (purpose_node) {
|
||||
vec.push_back(purpose_node);
|
||||
purpose_node = purpose_node->NextSiblingElement();
|
||||
}
|
||||
get_all_elements(vec);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -159,6 +181,7 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t
|
||||
if (last_item == nullptr) {
|
||||
return false;
|
||||
}
|
||||
// 检查重复性
|
||||
for (const auto& data : vec) {
|
||||
tinyxml2::XMLDocument doc;
|
||||
doc.Parse(data.c_str());
|
||||
@@ -166,6 +189,10 @@ bool CXmlOpr::import_newer_data(const std::vector<std::string>& vec, std::size_t
|
||||
if (item == nullptr) {
|
||||
continue;
|
||||
}
|
||||
const char* key_str = item->Attribute(keys_[0].c_str());
|
||||
if (check_key_exists(std::string(key_str))) {
|
||||
continue;
|
||||
}
|
||||
++success_count;
|
||||
auto* nitem = copy_element(item);
|
||||
insert_brother_node(last_item, nitem);
|
||||
@@ -183,10 +210,18 @@ bool CXmlOpr::check_key_exists(const Property_t& property)
|
||||
if (keys_.size() < 1 || property.size() < 1) {
|
||||
return false;
|
||||
}
|
||||
return check_key_exists(property[0].key);
|
||||
}
|
||||
|
||||
bool CXmlOpr::check_key_exists(const std::string& key)
|
||||
{
|
||||
if (keys_.size() < 1 || key.empty()) {
|
||||
return false;
|
||||
}
|
||||
Element_t* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
|
||||
while (purpose_node) {
|
||||
const char* value = purpose_node->Attribute(keys_[0].c_str());
|
||||
if (property[0].value == std::string(value)) {
|
||||
if (key == std::string(value)) {
|
||||
return true;
|
||||
}
|
||||
purpose_node = purpose_node->NextSiblingElement();
|
||||
|
||||
Reference in New Issue
Block a user