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 ...
随机推荐
- 第三节 - centos 内核启动、救援模式、 ls 、目录结构
Linux 第三节一.CentOS 启动: 1.内核引导: 1.win/linux 通电,2.BISO自检(CPU,内存,硬盘等 | U盘.光驱.网卡.硬盘启动 通过MBR知道内核内存硬件驱动位置并加 ...
- Android短视频SDK转码实践
一. 前言 一些涉及的基本概念: 转码:一般指多媒体文件格式的转换,比如分辨率.码率.封装格式等: 解复用(demux):从某种封装中分离出视频track和音频track,然后交给后续模块进行处理: ...
- 本机向windows服务器传输文件的三种方法
闲来无事,在腾讯云上申请了一个免费的服务器,想将自己写的网页发布到服务器上,服务器的申请很简单,百度搜索 腾讯云 ,然后新人第一次注册能申请到免费一个月的云主机,虽然配置不怎么高,但是还是能用的,这是 ...
- redis资料收集
http://www.runoob.com/redis/redis-sets.html redis set 使用 https://www.cnblogs.com/wanzaixiaoxin/p/49 ...
- requireJS对文件合并与压缩(二)
requireJS对文件合并与压缩 RequireJS提供了一个打包与压缩工具r.js,r.js的压缩工具使用UglifyJS进行压缩的或Closure Compiler.r.js下载 require ...
- Python类方法、静态方法与实例方法
静态方法是指类中无需实例参与即可调用的方法(不需要self参数),在调用过程中,无需将类实例化,直接在类之后使用.号运算符调用方法. 通常情况下,静态方法使用@staticmethod装饰器来声明. ...
- git上传文件到github
一.git之上传代码到github. 安装git,这个就不说了,很多帖子都有详细说明. 二.新建仓库,GitHub上的,首先申请账号. 三.本地选择地方新建本地仓库. 建完本地仓库文件夹,在本地 ...
- JMeter-自动生成测试报告
很多朋友都在问jmeter如何生成测试报告,这里随便说两句. 环境要求 1:jmeter3.0版本之后开始支持动态生成测试报表 2:jdk版本1.7以上 3:需要jmx脚本文件 基本操作 1:在你的脚 ...
- 前端学习:html基础学习四
7.HTML表格(主要内容<table><caption><tr><th><td>标记) <table>标记 基本格式 < ...
- 86、flask之一些凌乱知识点
本篇导航: session组件 上下文与内置函数 pymysql问题 模版问题 一.session组件 1.session组件简介 flask-session是flask框架的session组件,由于 ...