2024-05-15 10:59:43 +08:00
|
|
|
#include "xml_opr.h"
|
|
|
|
|
|
|
|
CXmlOpr::CXmlOpr() = default;
|
|
|
|
CXmlOpr::~CXmlOpr() = default;
|
|
|
|
|
|
|
|
bool CXmlOpr::open(const std::string &xml_path)
|
|
|
|
{
|
|
|
|
if (doc_.LoadFile(xml_path.c_str()) != tinyxml2::XML_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
2024-05-15 13:17:24 +08:00
|
|
|
xml_path_ = xml_path;
|
2024-05-15 10:59:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXmlOpr::set_baseinfo(const OprBase& base)
|
|
|
|
{
|
|
|
|
opr_base_ = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXmlOpr::parse_xml(std::vector<tinyxml2::XMLElement*>& vec)
|
|
|
|
{
|
|
|
|
std::string next_node{};
|
|
|
|
std::string node_path = opr_base_.node_path;
|
|
|
|
|
|
|
|
auto nodes = splitString(opr_base_.node_path, "/");
|
|
|
|
for (const auto& item : nodes) {
|
|
|
|
|
|
|
|
if (item.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-05-15 17:34:07 +08:00
|
|
|
if (parent_node_ == nullptr) {
|
|
|
|
parent_node_ = doc_.FirstChildElement(item.c_str());
|
2024-05-15 10:59:43 +08:00
|
|
|
}
|
|
|
|
else {
|
2024-05-15 17:34:07 +08:00
|
|
|
parent_node_ = parent_node_->FirstChildElement(item.c_str());
|
2024-05-15 10:59:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vec.clear();
|
2024-05-15 17:34:07 +08:00
|
|
|
element* purpose_node = parent_node_->FirstChildElement(opr_base_.the_node.c_str());
|
2024-05-15 10:59:43 +08:00
|
|
|
while (purpose_node)
|
|
|
|
{
|
|
|
|
vec.push_back(purpose_node);
|
|
|
|
purpose_node = purpose_node->NextSiblingElement();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2024-05-15 13:17:24 +08:00
|
|
|
|
2024-05-15 17:34:07 +08:00
|
|
|
void CXmlOpr::insert_brother_node(element* brother, element* newer)
|
|
|
|
{
|
|
|
|
if (!brother || !newer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parent_node_->InsertAfterChild(brother, newer);
|
|
|
|
}
|
|
|
|
|
|
|
|
element* CXmlOpr::copy_element(element* ele)
|
|
|
|
{
|
|
|
|
if (!ele) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
element* ret = doc_.NewElement(ele->Name());
|
|
|
|
const auto* attribute = ele->FirstAttribute();
|
|
|
|
while (attribute) {
|
|
|
|
ret->SetAttribute(attribute->Name(), attribute->Value());
|
|
|
|
attribute = attribute->Next();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-05-15 13:17:24 +08:00
|
|
|
bool CXmlOpr::save()
|
|
|
|
{
|
|
|
|
auto ret = doc_.SaveFile(xml_path_.c_str());
|
|
|
|
if (ret != tinyxml2::XML_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|