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读写的更多相关文章

  1. 【Python】Python XML 读写

    class ACTIVE_FILE_PROTECT_RULE_VIEW(APIView): renderer_classes = (JSONRenderer, BrowsableAPIRenderer ...

  2. XML读写工具

    import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...

  3. C# XML读写实例

    一.使用System.Xml 实例:完成如下格式配置文件的读写操作: <?xml version="1.0" encoding="UTF-8"?> ...

  4. 网站的配置文件XML读写

    网站的配置信息一般都写入到XML中,以下是简单的对xml的读写操作,仅供参考. 读操作: XmlDocument xmlDoc = new XmlDocument(); XmlReaderSettin ...

  5. 评论模块Demo(XML读写,定时器。)

    这个Demo主要是自己做练习熟悉jquery,ajax,与xml文件的读写,以下是实现页面效果: 后台控制器: public ActionResult AddMsg() { XmlDocument x ...

  6. Python之xml读写

    遇到问题xml文件读写,没有子节点需要新建ChildNode. # -*- coding: utf-8 -*- import os import shutil import xml.dom.minid ...

  7. php XML 读写 创建

    一 .XML 读 1.1. 首先同目录定义好一个XML文件 : book.xml <?xml version="1.0" encoding="utf-8" ...

  8. xml读写文件实例

    在某个通讯中需要向服务器发送请求xml,格式例子如下: <?xml version="1.0" encoding="UTF-8"?> <ROO ...

  9. XML读写

    private string fileName = HttpContext.Current.Server.MapPath("~/Student.xml"); protected v ...

随机推荐

  1. java方向及学习方法

    随笔:由于回首最近刚刚上班的缘故,平时基本没时间上播客了,所以回首会定期的抽时间分享一些干货给朋友们,就是周期不会像之前那么频繁了.最近有朋友跟回首说想没事儿的时候自学Java,但苦于不知道怎么去学, ...

  2. 记录一个从没见过的bug

    js的默认启动 $(function(){ )}; 不识别,意思是如果你把js内容放入这个东西里面,它不会执行.必须把它去掉才可以. 包括.tag里的文件也是一样. 这是发生在系统框架迁移发生的事,以 ...

  3. Django_调查问卷

    1.问卷的保存按钮 前端通过ajax把数据发过来后端处理数据,然后返回给前端2.对问卷做答 首先用户进行登录,验证 条件:1.只有本班的学生才能对问卷做答      2.已经参加过的不能再次访问   ...

  4. 前端之JavaScript--基础

    JavaScript 独立的语言,浏览器具有js解释器 一. JavaScript代码存在形式: - Head中 <script> //javascript代码 alert(123); & ...

  5. Ruby学习之对象模型

    这两周工作内容较多,平时自己也有点不在状态,学的东西有点少了,趁着现在还有点状态,赶紧复习一下之前学习的Ruby吧. Ruby是我真正开始接触动态语言魅力的第一个语言,之前虽然也用过且一直用perl. ...

  6. DBCC page 数据页 堆 底层数据分布大小计算

    1.行的总大小: Row_Size = Fixed_Data_Size + Variable_Data_Size + Null_Bitmap + 4(4是指行标题开销) 开销定义: Fixed_Dat ...

  7. [Spark内核] 第31课:Spark资源调度分配内幕天机彻底解密:Driver在Cluster模式下的启动、两种不同的资源调度方式源码彻底解析、资源调度内幕总结

    本課主題 Master 资源调度的源码鉴赏 [引言部份:你希望读者看完这篇博客后有那些启发.学到什么样的知识点] 更新中...... 资源调度管理 任务调度与资源是通过 DAGScheduler.Ta ...

  8. iOS开发的另类神器:libimobiledevice开源包【类似android adb 方便获取iOS设备信息】

    简介 libimobiledevice又称libiphone,是一个开源包,可以让Linux支持连接iPhone/iPod Touch等iOS设备.由于苹果官方并不支持Linux系统,但是Linux上 ...

  9. 配置不同环境下启用swagger,在生产环境关闭swagger

    前言 Swagger使用起来简单方便,几乎所有的API接口文档都采用swagger了.使用示例:http://www.cnblogs.com/woshimrf/p/swagger.html, 现在开发 ...

  10. vscode 开发工具

    做开发两年了,而我记忆力不太好,所以写代码得靠强大的编辑器提示. 陆陆续续使用了如 notepad++.dreamweaver.sublime text.webstorm.phpstorm.Atom等 ...