加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

UE4 用FFastXml解析Xml

发布时间:2020-12-16 08:05:12 所属栏目:百科 来源:网络整理
导读:原文链接:http://blog.csdn.net/u011047958/article/details/78861469 ?解析文件的引用: FFastXml存在于Runtime/XmlParser/Public/FastXml.h头文件中 API Document中关于FFastXml的用法说的还是很清楚的,因此,本篇之简要说明一下基本使用规则: 首先,你

原文链接:http://blog.csdn.net/u011047958/article/details/78861469

?解析文件的引用:
FFastXml存在于Runtime/XmlParser/Public/FastXml.h头文件中
API Document中关于FFastXml的用法说的还是很清楚的,因此,本篇之简要说明一下基本使用规则:
首先,你必须实现IFastXmlCallback接口,事实上,你的主要操作就在这个接口提供的5个接口函数中。
基于此,继承IFastXmlCallback接口后,你需要实现以下5个接口函数,它们会在读取XML时被回调:

virtualboolProcessAttribute(constTCHAR*AttributeName,constAttributeValue)override;//Xml属性处理

virtualboolProcessClose(constElement)override;//Xml Element结束时处理

virtualboolProcessComment(constElement)override;//Xml注释处理

virtualboolProcessElement(constElementName,128);min-height:11pt;">ElementData,int32XmlFileLineNumber)override;//子节点处理

virtualboolProcessXmlDeclaration(constXmlFileLineNumber)override;//声明处理

核心函数:
bool ParseXmlFile(class IFastXmlCallback,TCHAR* XmlFilePath,TCHAR* XmlFileContents,FFeedbackContext*,bool,FText&,int32&);
//这里只说明关键的两个参数XmlFilePath:就是Xml的所在路径,TCHAR* XmlFileContents:Xml文档内容,这两个参数可以用两种方式的,其中一个为空的
情况下,另一需要非空。
xmlFilePath非空,那么会从该路径下读取Xml文件,适用于本地Xml文件的读取。
xmlFileContents非空,适用于项目工程内部Xml字符串的读取。

FFastXml解析的一种封装与使用:
//XmlHandleComponent.h
#pragmaonce
#include"CoreMinimal.h"
"FastXml.h"
"XmlHandleComponent.generated.h"
classUActorComponent;
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FProcessAttributeDelegate,FString,AttributeName,AttributeValue);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FProcessCloseDelegate,Element);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FProcessCommentDelegate,Element);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FProcessElementDelegate,ElementName,ElementData,175);min-height:11pt;">int32,XmlFileLineNumber);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FProcessXmlDeclarationDelegate,XmlFileLineNumber);
UCLASS(meta = (BlueprintSpawnableComponent))
classUXmlHandleComponent:publicUActorComponent,publicIFastXmlCallback
{
GENERATED_BODY()
public:
UXmlHandleComponent();
~XmlHandleComponent();
virtualboolProcessAttribute(constAttributeValue)override;
virtualboolProcessClose(constElement)override;
virtualboolProcessComment(constElement)override;
virtualboolProcessElement(constXmlFileLineNumber)override;
virtualboolProcessXmlDeclaration(constXmlFileLineNumber)override;
UFUNCTION(BlueprintCallable,Category="XmlParser")
boolParseXmlFileFromPath(constFString&Path,boolbShowSlowTaskDialog,128);min-height:11pt;">bShowCancelButton);
UFUNCTION(BlueprintCallable,Category ="XmlParser")
boolParseXmlFileFromContent(FString&Content,128);min-height:11pt;">bShowCancelButton);
public:
UPROPERTY(BlueprintAssignable,21);min-height:11pt;">"XmlParser")
FProcessAttributeDelegateProcessAttributeDelegate;
UPROPERTY(BlueprintAssignable,175);min-height:11pt;">FProcessCloseDelegateProcessCloseDelegate;
UPROPERTY(BlueprintAssignable,175);min-height:11pt;">FProcessCommentDelegateProcessCommentDelegate;
UPROPERTY(BlueprintAssignable,175);min-height:11pt;">FProcessElementDelegateProcessElementDelegate;
UPROPERTY(BlueprintAssignable,175);min-height:11pt;">FProcessXmlDeclarationDelegateProcessXmlDeclarationDelegate;

};
//XmlHandleComponent.cpp
"UXmlHandleComponent.h"
"Components/ActorComponent.h"
"Paths.h"
XmlHandleComponent::XmlHandleComponent()
{
}
XmlHandleComponent::~XmlHandleComponent()
{
}
boolXmlHandleComponent::ProcessAttribute(constAttributeValue)
{
ProcessAttributeDelegate.Broadcast(FString(AttributeName),128);min-height:11pt;">AttributeValue));
returntrue;
}
boolXmlHandleComponent::ProcessClose(constElement)
{
ProcessCloseDelegate.Broadcast(Element));
returntrue;
}
boolXmlHandleComponent::ProcessComment(constElement)
{
ProcessCommentDelegate.Broadcast(XmlHandleComponent::ProcessElement(constXmlFileLineNumber)
{
ProcessElementDelegate.Broadcast(ElementName),128);min-height:11pt;">ElementData),XmlFileLineNumber);
returntrue;
}
boolXmlHandleComponent::ProcessXmlDeclaration(constXmlFileLineNumber)
{
ProcessXmlDeclarationDelegate.Broadcast(XmlHandleComponent::ParseXmlFileFromPath(constboolbShowSlowTaskDialog=false,128);min-height:11pt;">bShowCancelButton=false)
{
booltempBool=FFastXml::ParseXmlFile(this,*NULL,nullptr,128);min-height:11pt;">bShowCancelButton,mOutErrorMessage,OutErrorLineNumber);
returntempBool;
}
boolXmlHandleComponent::ParseXmlFileFromContent(bShowSlowTaskDialog=false,128);min-height:11pt;">bShowCancelButton=false)
{
booltempBool =Content.GetCharArray().GetData(),OutErrorLineNumber);
returntempBool;
}
该方法把处理操作转交给了5个动态的多播委托,并暴露给蓝图,如果你愿意,也可以添加5个BlueprintNativeEvent式的函数,并绑定到委托上,取消委托的UPROPERTY()暴露声明,这样就能让C++和蓝图都能得到修改。另外使用组件的方式,更能方便使用和卸载,当然,组件继承的父组件取决于你个人的需求。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读