解析文件的引用:
FFastXml存在于Runtime/XmlParser/Public/FastXml.h头文件中
API Document中关于FFastXml的用法说的还是很清楚的,因此,本篇之简要说明一下基本使用规则:
首先,你必须实现IFastXmlCallback接口,事实上,你的主要操作就在这个接口提供的5个接口函数中。
基于此,继承IFastXmlCallback接口后,你需要实现以下5个接口函数,它们会在读取XML时被回调:
virtual bool ProcessAttribute(const TCHAR* AttributeName, const TCHAR* AttributeValue) override;//Xml属性处理
virtual bool ProcessClose(const TCHAR* Element)override;//Xml Element结束时处理
virtual bool ProcessComment(const TCHAR* Element)override;//Xml注释处理
virtual bool ProcessElement(const TCHAR* ElementName, const TCHAR* ElementData, int32 XmlFileLineNumber)override;//子节点处理
virtual bool ProcessXmlDeclaration(const TCHAR* ElementData, int32 XmlFileLineNumber)override;//声明处理
核心函数:
bool ParseXmlFile(class IFastXmlCallback,TCHAR* XmlFilePath,TCHAR* XmlFileContents,FFeedbackContext*,bool,bool,FText&,int32&);
//这里只说明关键的两个参数XmlFilePath:就是Xml的所在路径,TCHAR* XmlFileContents:Xml文档内容,这两个参数可以用两种方式的,其中一个为空的
情况下,另一需要非空。
xmlFilePath非空,那么会从该路径下读取Xml文件,适用于本地Xml文件的读取。
xmlFileContents非空,适用于项目工程内部Xml字符串的读取。
FFastXml解析的一种封装与使用:
//XmlHandleComponent.h
#pragma once
#include "CoreMinimal.h"
#include "FastXml.h"
#include "XmlHandleComponent.generated.h"
class UActorComponent;
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FProcessAttributeDelegate,FString,AttributeName,FString,AttributeValue);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FProcessCloseDelegate,FString,Element);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FProcessCommentDelegate, FString,Element);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_ThreeParams(FProcessElementDelegate,FString,ElementName,FString,ElementData,int32,XmlFileLineNumber);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FProcessXmlDeclarationDelegate,FString,ElementData,int32,XmlFileLineNumber);
UCLASS(meta = (BlueprintSpawnableComponent))
class UXmlHandleComponent:public UActorComponent, public IFastXmlCallback
{
GENERATED_BODY()
public:
UXmlHandleComponent();
~UXmlHandleComponent();
virtual bool ProcessAttribute(const TCHAR* AttributeName, const TCHAR* AttributeValue) override;
virtual bool ProcessClose(const TCHAR* Element)override;
virtual bool ProcessComment(const TCHAR* Element)override;
virtual bool ProcessElement(const TCHAR* ElementName, const TCHAR* ElementData, int32 XmlFileLineNumber)override;
virtual bool ProcessXmlDeclaration(const TCHAR* ElementData, int32 XmlFileLineNumber)override;
UFUNCTION(BlueprintCallable,Category="XmlParser")
bool ParseXmlFileFromPath(const FString& Path, bool bShowSlowTaskDialog, bool bShowCancelButton);
UFUNCTION(BlueprintCallable, Category = "XmlParser")
bool ParseXmlFileFromContent(FString &Content, bool bShowSlowTaskDialog, bool bShowCancelButton);
public:
UPROPERTY(BlueprintAssignable, Category = "XmlParser")
FProcessAttributeDelegate ProcessAttributeDelegate;
UPROPERTY(BlueprintAssignable, Category = "XmlParser")
FProcessCloseDelegate ProcessCloseDelegate;
UPROPERTY(BlueprintAssignable, Category = "XmlParser")
FProcessCommentDelegate ProcessCommentDelegate;
UPROPERTY(BlueprintAssignable,Category = "XmlParser")
FProcessElementDelegate ProcessElementDelegate;
UPROPERTY(BlueprintAssignable,Category="XmlParser")
FProcessXmlDeclarationDelegate ProcessXmlDeclarationDelegate;
};
//XmlHandleComponent.cpp
#include "UXmlHandleComponent.h"
#include "Components/ActorComponent.h"
#include "FastXml.h"
#include "Paths.h"
UXmlHandleComponent::UXmlHandleComponent()
{
}
UXmlHandleComponent::~UXmlHandleComponent()
{
}
bool UXmlHandleComponent::ProcessAttribute(const TCHAR* AttributeName, const TCHAR* AttributeValue)
{
ProcessAttributeDelegate.Broadcast(FString(AttributeName),FString(AttributeValue));
return true;
}
bool UXmlHandleComponent::ProcessClose(const TCHAR* Element)
{
ProcessCloseDelegate.Broadcast(FString(Element));
return true;
}
bool UXmlHandleComponent::ProcessComment(const TCHAR* Element)
{
ProcessCommentDelegate.Broadcast(FString(Element));
return true;
}
bool UXmlHandleComponent::ProcessElement(const TCHAR* ElementName, const TCHAR* ElementData, int32 XmlFileLineNumber)
{
ProcessElementDelegate.Broadcast(FString(ElementName),FString(ElementData),XmlFileLineNumber);
return true;
}
bool UXmlHandleComponent::ProcessXmlDeclaration(const TCHAR* ElementData, int32 XmlFileLineNumber)
{
ProcessXmlDeclarationDelegate.Broadcast(FString(ElementData),XmlFileLineNumber);
return true;
}
bool UXmlHandleComponent::ParseXmlFileFromPath(const FString& Path,bool bShowSlowTaskDialog=false, bool bShowCancelButton=false)
{
bool tempBool=FFastXml::ParseXmlFile(this,*Path,NULL,nullptr,bShowSlowTaskDialog,bShowCancelButton,mOutErrorMessage,OutErrorLineNumber);
return tempBool;
}
bool UXmlHandleComponent::ParseXmlFileFromContent(FString &Content,bool bShowSlowTaskDialog = false, bool bShowCancelButton = false)
{
bool tempBool = FFastXml::ParseXmlFile(this,nullptr,Content.GetCharArray().GetData(),nullptr,bShowSlowTaskDialog,bShowCancelButton,mOutErrorMessage,OutErrorLineNumber);
return tempBool;
}
该方法把处理操作转交给了5个动态的多播委托,并暴露给蓝图,如果你愿意,也可以添加5个BlueprintNativeEvent式的函数,并绑定到委托上,取消委托的UPROPERTY()暴露声明,这样就能让C++和蓝图都能得到修改。另外使用组件的方式,更能方便使用和卸载,当然,组件继承的父组件取决于你个人的需求