日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UE4 Xml读写

發布時間:2025/3/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.h

xmlobject.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

總結

以上是生活随笔為你收集整理的UE4 Xml读写的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。