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 = ,...);
~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(-, 10.0f, FColor::Red, TEXT("打开Xml文件失败啦"));
loadFileSuccess = false;
}
else
{
m_RootNode = m_File->GetRootNode(); if (m_RootNode == nullptr)
{
GEngine->AddOnScreenDebugMessage(-, 10.0f, FColor::Red, TEXT("获取根节点失败啦")); const FString XmlRootNodeContent = "<RootNode>\n</RootNode>";
m_File = new FXmlFile(XmlRootNodeContent, EConstructMethod::ConstructFromBuffer); if (m_File == nullptr)
{
GEngine->AddOnScreenDebugMessage(-, 10.0f, FColor::Red, TEXT("创建Xml文件失败啦"));
loadFileSuccess = false;
}
else
{
GEngine->AddOnScreenDebugMessage(-, 10.0f, FColor::Green, TEXT("创建Xml文件成功啦"));
m_RootNode = m_File->GetRootNode(); if (NodeCount == )
{
GEngine->AddOnScreenDebugMessage(-, 10.0f, FColor::Red, TEXT("没有创建默认Xml节点"));
loadFileSuccess = true;
}
va_list arg_ptr;
va_start(arg_ptr, NodeCount); for (int i = ; 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(-, 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(-, 10.0f, FColor::Red, TEXT("不存在该Node")); m_RootNode->AppendChildNode(tag, content); if (m_RootNode->FindChildNode(tag) == nullptr)
{
GEngine->AddOnScreenDebugMessage(-, 10.0f, FColor::Green, TEXT("创建Node失败"));
return false;
}
else
{
GEngine->AddOnScreenDebugMessage(-, 10.0f, FColor::Green, TEXT("创建Node成功"));
this->Save();
return true;
}
}
else
{
FindNode->SetContent(content);
GEngine->AddOnScreenDebugMessage(-, 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(-, 10.0f, FColor::Red, TEXT("该节点不存在,无法给该节点添加子节点"));
return false;
}
else
{
ParentNode->AppendChildNode(ChildNodeTag, ChildNodeContent);
if (ParentNode->FindChildNode(ChildNodeTag) == nullptr)
{
GEngine->AddOnScreenDebugMessage(-, 10.0f, FColor::Red, TEXT("子节点创建失败"));
return false;
}
else
{
GEngine->AddOnScreenDebugMessage(-, 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(-, 10.0f, FColor::Red, TEXT("查找该Node失败"));
//错误代码2222
const TCHAR* tempChar = *FString("");
return tempChar;
}
else
{
const TCHAR* tempChar = *(findNode->GetContent());
return tempChar;
}
}
xmlobject.cpp
UE4 Xml读写的更多相关文章
- 【Python】Python XML 读写
class ACTIVE_FILE_PROTECT_RULE_VIEW(APIView): renderer_classes = (JSONRenderer, BrowsableAPIRenderer ...
- XML读写工具
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...
- C# XML读写实例
一.使用System.Xml 实例:完成如下格式配置文件的读写操作: <?xml version="1.0" encoding="UTF-8"?> ...
- 网站的配置文件XML读写
网站的配置信息一般都写入到XML中,以下是简单的对xml的读写操作,仅供参考. 读操作: XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettin ...
- 评论模块Demo(XML读写,定时器。)
这个Demo主要是自己做练习熟悉jquery,ajax,与xml文件的读写,以下是实现页面效果: 后台控制器: public ActionResult AddMsg() { XmlDocument x ...
- Python之xml读写
遇到问题xml文件读写,没有子节点需要新建ChildNode. # -*- coding: utf-8 -*- import os import shutil import xml.dom.minid ...
- php XML 读写 创建
一 .XML 读 1.1. 首先同目录定义好一个XML文件 : book.xml <?xml version="1.0" encoding="utf-8" ...
- xml读写文件实例
在某个通讯中需要向服务器发送请求xml,格式例子如下: <?xml version="1.0" encoding="UTF-8"?> <ROO ...
- XML读写
private string fileName = HttpContext.Current.Server.MapPath("~/Student.xml"); protected v ...
随机推荐
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第3讲(玩家发射子弹)
一.公布上一讲中玩家飞机上.下.右移动实现的代码: /*=========================键盘按下事件 keycode为得到键盘相应键对应的数字==================== ...
- Anaconda系统中管理程序包(Package)
列出所有已安装的程序包 conda list 在已安装的程序包中查找某个特定的程序包 conda search package-name 安装程序包 conda install beautiful-s ...
- VMware_ubuntu设置共享文件夹
1. 点击安装VMware tools 2.将/media/vmtool的压缩包复制到/home/pc/vm_tool下,应为原路径在root权限下竟然也是只读的,并且无法更改. 3.进入/home/ ...
- jq 中each的用法 (share)
each的使用方法 在jQuery里有一个each方法,用起来非常的爽,不用再像原来那样写for循环,jQuery源码里自己也有很多用到each方法.其实jQuery里的each方法是通过js里的ca ...
- Struts简介、原理及简单实现
struts简介 Struts是Apache软件基金会(ASF)赞助的一个开源项目.它最初是jakarta项目中的一个子项目,并在2004年3月成为ASF的顶级项目.它通过采用JavaServlet/ ...
- python导入模块时的执行顺序
当python导入模块,执行import语句时,到底进行了什么操作?按照python的文档,她执行了如下的操作: 第一步,创建一个新的module对象(它可能包含多个module) 第二步,把这个mo ...
- 在macOS上通过pyenv安装和切换多版本Python
1. 安装homebrew 官网 http://brew.sh/index_zh-cn.html 打开终端,在终端中粘贴如下脚本 /usr/bin/ruby -e "$(curl -fsSL ...
- linux sshd服务
1.ssh介绍:SSH是secure shell protocol的简写,由IETF网络工作小组制定,在进行数据传输之前,SSH先对联机数据包通过加密技术进行加密处理,加密后再进行传输,确保传递的数据 ...
- 分享一个大神自己的blog
std::sort() 详解 http://feihu.me/blog/ C++11 新特性 http://blog.guoyb.com/2016/09/19/cpp11-all/ unity3d 相 ...
- Java集合系列[1]----ArrayList源码分析
本篇分析ArrayList的源码,在分析之前先跟大家谈一谈数组.数组可能是我们最早接触到的数据结构之一,它是在内存中划分出一块连续的地址空间用来进行元素的存储,由于它直接操作内存,所以数组的性能要比集 ...