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. 基于telegraf+influxdb+grafana进行postgresql数据库监控

    前言 随着公司postgresql数据库被广泛应用,尤其是最近多个项目在做性能测试的时候都是基于postgresql的数据库,为了确定性能瓶颈是否会出现在数据库中,数据库监控也被我推上了日程.在网上找 ...

  2. windows日志监控

    bat脚本,主要作用,每个五分钟读取日文本件中新增内容,进行错误赛选,如果有错误信息,将错误信息用邮件发送给管理员. 其中awk和sed需要手动下载 :读取number.txt文档,获取上一次执行时文 ...

  3. Java 包装类Integer的值比较

    对于包装类型Integer的值比较与int的值比较是不同的:   public class Java_Val_Compare { public static void main(String[] ar ...

  4. C# (类型、对象、线程栈和托管堆)在运行时的相互关系

    在介绍运行时的关系之前,先从一些计算机基础只是入手,如下图: 该图展示了已加载CLR的一个windows进程,该进程可能有多个线程,线程创建时会分配到1MB的栈空间.栈空间用于向方法传递实参,方法定义 ...

  5. 保存html上传文件过程中遇到的字节流和字符流问题总结

    java字节流和字符流的区别以及相同 1. 字节流文件本身进行操作,字符流是通过缓存进行操作, 1.1 使用字节流不执行关闭操作 File f =new File("d:/test/test ...

  6. centos7 编译ntopng源码

    先安装编译所需的开发工具 yum groupinstall 'Development Tools' yum install tcl yum install libpcap libpcap-devel ...

  7. 程序猿的日常——HashMap的相关知识

    背景知识 哈希冲突 哈希是指通过某种方法把数据转变成特定的数值,数值根据mod对应到不同的单元上.比如在Java中,字符串就是通过每个字符的编码来计算.数字是本身对应的值等等,不过就算是再好的哈希方法 ...

  8. MySQL视图,触发器,事务,存储过程,函数

    create triggr triafterinsertcmdlog after insert on cmd_log FOR EACH ROW trigger_body .#NEW : 代表新的记录 ...

  9. yield next和yield* next的区别

    yield next和yield* next之间到底有什么区别?为什么需要yield* next?经常会有人提出这个问题.虽然我们在代码中会尽量避免使用yield* next以减少新用户的疑惑,但还是 ...

  10. win8使用putty登录虚拟机linux

    从下午两点开始在尝试,差不多用了6个小时候到现在终于成功了! 连接器使用的是putty,只要知道虚拟机的ip地址就可以尝试连接,所以首先查询虚拟机上的ip地址,使用命令: ifconfig 出现提示: ...