微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UE4 用FFastXml解析Xml

原文链接: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++和蓝图都能得到修改。另外使用组件的方式,更能方便使用和卸载,当然,组件继承的父组件取决于你个人的需求。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。