UE4 Xml读写
UE4自帶一個XmlParser,可以很方便的實現Xml的讀寫。
1,在PublicDependencyModuleNames.AddRange中添加XmlParser。
2,include XmlParser.h
讀寫操作封裝在了xmlobject? 需要根據需求增加 修改
xmlobject.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h" #include <map>/*** */ struct NodeStruct {FString tag;FString content;NodeStruct(FString Tag,FString Content){tag = Tag;content = Content;} };class TESTJIGOU_API XmlFileObject { public:XmlFileObject(const FString &filePath, const FString &fileName,int NodeCount = 0,...);~XmlFileObject();public:class FXmlFile* m_File;class FXmlNode* m_RootNode;FString m_FilePath;FString m_FileName;bool loadFileSuccess;public:bool SetNode(const FString &tag, const FString &content);bool SetNode(const FString &tag, int content);bool SetNode(const FString &tag, float content);bool AddChild(const FString &ParentNodeTag,const FString& ChildNodeTag,const FString &ChildNodeContent);bool AddChild(FXmlNode* ParentNode, const FString& ChildNodeTag, const FString& ChildNodeContent);FXmlNode* GetNode(const FString& tag,const FString &content);FXmlNode* GetChildNode(FXmlNode* TargetNode, const FString& ChildTag);FXmlNode* GetChildNode(FXmlNode* TargetNode, const FString& ChildTag, const FString& ChildContent);const TCHAR* GetChildNodeContent(FXmlNode* TargetNode, const FString& ChildTag);const TCHAR* GetNodeContent(const FString &tag);private:void Save(); }; xmlobject.hxmlobject.cpp
// Fill out your copyright notice in the Description page of Project Settings. #include "XmlFileObject.h" #include "XmlParser.h" #include "Engine.h" #include "stdarg.h"XmlFileObject::XmlFileObject(const FString &filePath, const FString &fileName,int NodeCount, ...) : m_FileName(fileName), m_FilePath(filePath) {m_File = new FXmlFile(filePath + fileName);if (m_File == nullptr){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("打開Xml文件失敗啦"));loadFileSuccess = false;}else{m_RootNode = m_File->GetRootNode();if (m_RootNode == nullptr){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("獲取根節點失敗啦"));const FString XmlRootNodeContent = "<RootNode>\n</RootNode>";m_File = new FXmlFile(XmlRootNodeContent, EConstructMethod::ConstructFromBuffer);if (m_File == nullptr){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("創建Xml文件失敗啦"));loadFileSuccess = false;}else{GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("創建Xml文件成功啦"));m_RootNode = m_File->GetRootNode();if (NodeCount == 0){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("沒有創建默認Xml節點"));loadFileSuccess = true;}va_list arg_ptr;va_start(arg_ptr, NodeCount);for (int i = 0; i < NodeCount; i++){auto node = va_arg(arg_ptr, NodeStruct);SetNode(node.tag, node.content);}va_end(arg_ptr);loadFileSuccess = true;this->Save();}}else{loadFileSuccess = true;this->Save();GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("打開Xml文件成功啦"));}} }XmlFileObject::~XmlFileObject() { }void XmlFileObject::Save() {m_File->Save(m_FilePath + m_FileName); }bool XmlFileObject::SetNode(const FString &tag, const FString &content) {FXmlNode* FindNode = m_RootNode->FindChildNode(tag);if (FindNode == nullptr){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("不存在該Node"));m_RootNode->AppendChildNode(tag, content);if (m_RootNode->FindChildNode(tag) == nullptr){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("創建Node失敗"));return false;}else{GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("創建Node成功"));this->Save();return true;}}else{FindNode->SetContent(content);GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("設置Node成功"));this->Save();return true;} }bool XmlFileObject::SetNode(const FString &tag, int content) {return this->SetNode(tag, FString::FromInt(content)); }bool XmlFileObject::SetNode(const FString &tag, float content) {return this->SetNode(tag, FString::SanitizeFloat(content)); }bool XmlFileObject::AddChild(const FString &ParentNodeTag, const FString& ChildNodeTag, const FString &ChildNodeContent) {auto ParentNode = m_RootNode->FindChildNode(ParentNodeTag);return this->AddChild(ParentNode, ChildNodeTag, ChildNodeContent); }bool XmlFileObject::AddChild(FXmlNode* ParentNode, const FString& ChildNodeTag, const FString& ChildNodeContent) {if (ParentNode == nullptr){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("該節點不存在,無法給該節點添加子節點"));return false;}else{ParentNode->AppendChildNode(ChildNodeTag, ChildNodeContent);if (ParentNode->FindChildNode(ChildNodeTag) == nullptr){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("子節點創建失敗"));return false;}else{GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("子節點創建成功"));this->Save();return true;}} }FXmlNode* XmlFileObject::GetNode(const FString& tag, const FString &content) { // auto FindNodeList = m_RootNode->GetChildrenNodes(); // // for (auto node : FindNodeList) // { // if (node->GetContent().Equals(content) && node->GetTag().Equals(tag)) // { // return node; // } // } // return nullptr;return this->GetChildNode(m_RootNode, tag, content); }FXmlNode* XmlFileObject::GetChildNode(FXmlNode* TargetNode, const FString& ChildTag, const FString& ChildContent) {auto FindNodeList = TargetNode->GetChildrenNodes();for (auto node : FindNodeList){if (node->GetContent().Equals(ChildContent) && node->GetTag().Equals(ChildTag)){return node;}}return nullptr; }FXmlNode* XmlFileObject::GetChildNode(FXmlNode* TargetNode, const FString& ChildTag) {auto FindNodeList = TargetNode->GetChildrenNodes();for (auto node : FindNodeList){if (node->GetTag().Equals(ChildTag)){return node;}}return nullptr; }const TCHAR* XmlFileObject::GetChildNodeContent(FXmlNode* TargetNode, const FString& ChildTag) {const TCHAR* result = *(GetChildNode(TargetNode, ChildTag)->GetContent());return result; }const TCHAR* XmlFileObject::GetNodeContent(const FString &tag) {FXmlNode* findNode = m_RootNode->FindChildNode(tag);if (findNode == nullptr){GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("查找該Node失敗"));//錯誤代碼2222const TCHAR* tempChar = *FString("2222");return tempChar;}else{const TCHAR* tempChar = *(findNode->GetContent());return tempChar;} } xmlobject.cpp?
轉載于:https://www.cnblogs.com/litmin/p/7447398.html
總結
- 上一篇: 【例题5-7 UVA - 136】Ugl
- 下一篇: 《Android虚拟机》----虚拟机概