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 ...
随机推荐
- 控制input 输入框的placeholder
/*webkit placeholder居右*/ ::-webkit-input-placeholder { color: #e7e7e7; text-indent: .3rem; font-size ...
- solr安装配置
1.solr是基于tomcat安装部署的 2.网上下载solr-5.2.1 http://lucene.apache.org/solr/downloads.html 3.解压solr文件 tar zx ...
- Jmeter 创建FTP测试计划
FTP服务主要提供上传和下载功能. 操作步骤: 1.创建一个线程组 2.线程组--->添加--->配置元件--->FTP请求缺省值:输入服务器名称或IP. 3.线程组--->添 ...
- css3整理-方便查询使用
最近详细地研究了CSS3的相关内容,并整理了这个文档,方便以后查询使用,分享给大家. 案例代码大家可以下载参考下:https://gitee.com/LIULIULIU8/CSS3 1.边框属性bor ...
- 尤克里里 ukulele 单板 非kaka tom uma
本店冲人气优惠,不搞倒闭之类的事 23寸尤克里里 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 单板 单板 单板 彩贝镶边演出大气 配件选购40元全套(加棉琴包.金属变调夹. ...
- Java容器---Map基础
1.Map API (1)Map 集合类用于存储元素对(称作"键"和"值"),其中每个键映射到一个值. java.util Interface Map<K ...
- 通过渲染改变tabBarItem的背景图片
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #703daa } p.p2 { margin: 0.0px 0. ...
- Ubuntu 16.04 LTS运行robo3t报错
系统环境:Ubuntu 16.04 LTS. 安装robomongo Robo 3T,运行时报以下错误: jaxu@jaxu-ubuntu:/usr/local/share/robo3t--linux ...
- C盘里的桌面文件移到E盘里了,然后E盘里的文件都显示到桌面上了,怎么将桌面文件还原回C盘
1 . 直接按Windows键+R,打开"运行"对话框,在输入框中输入"regedit"命令,会打开注册表编辑窗口: 2.打开注册表文件将HKEY_CURREN ...
- Java学习笔记27(集合框架一:ArrayList回顾、Collection接口方法)
集合:集合是java中提供的一种容器,可以用来存储多个数据 集合和数组的区别: 1.数组的长度是固定的,集合的长度是可变的 2.集合中存储的元素必须是引用类型数据 对ArrayList集合的回顾 示例 ...