UE XML解析

bool UHermesFunctionLibrary::isFileExist(const FString& Path)
{
	return FPlatformFileManager::Get().GetPlatformFile().FileExists(*Path);
}

FString UHermesFunctionLibrary::ReadXmlFile(const FString& Path, bool& isSuccess)
{
	if(!isFileExist(Path)){
		isSuccess = false;
		return FString("");
	}
	TSharedPtr<FXmlFile> XmlFile = MakeShareable(new FXmlFile(Path));
	if(!XmlFile->IsValid()){
		isSuccess = false;
		return FString("");
	}
	FString Content;
	FXmlNode* RootNode = XmlFile->GetRootNode();
	for(FXmlNode* ChildNode : RootNode->GetChildrenNodes())
	{
		FString strValue = ChildNode->GetAttribute("category");
		for(FXmlNode* SubChildNode : ChildNode->GetChildrenNodes())
		{
			FString subStrValue = SubChildNode->GetAttribute("lang");
			Content = SubChildNode->GetContent();
			if(!isSuccess)	isSuccess = true;
		}
	}
	return Content;
}

FString UHermesFunctionLibrary::ReadXmlFile(const FString& Path, const FString& Tag, bool& isSuccess)
{
	TSharedPtr<FXmlFile> XmlFile = MakeShareable(new FXmlFile(Path));
	if(!XmlFile->IsValid() || !isFileExist(Path)){
		isSuccess = false;
		return FString("");
	}
	FXmlNode* RootNode = XmlFile->GetRootNode();
	for(FXmlNode* ChildNode : RootNode->GetChildrenNodes())
	{
		if(ChildNode->GetTag() == Tag)	return ChildNode->GetContent();
	}

	return FString("");
}

 

这里如果要GetTag()则得改成 == title


FString UHermesFunctionLibrary::ReadXmlFile(const FString& Path, const FString& Tag, bool& isSuccess)//暂时做两层用于XML读取,以后升级
{
	TSharedPtr<FXmlFile> XmlFile = MakeShareable(new FXmlFile(Path));
	if(!XmlFile->IsValid() || !isFileExist(Path)){
		isSuccess = false;
		return FString("");
	}
	FXmlNode* RootNode = XmlFile->GetRootNode();
	for(FXmlNode* ChildNode : RootNode->GetChildrenNodes())
	{
		if (ChildNode->GetAttribute("category") == "COOKING")	return ChildNode->GetContent();
		//if(ChildNode->GetTag() == Tag)	return ChildNode->GetContent();
	}

	return FString("");
}

PrivateDependencyModuleNames.AddRange(new string[] { "XmlParser" });加载.cs里面


FXmlNode* UHermesFunctionLibrary::FindXmlNode(FXmlNode* RootNode, const FString& Contribution) {
	if (RootNode == nullptr)	return nullptr;
	for (FXmlNode* ChildNode : RootNode->GetChildrenNodes())
	{
		for (const FXmlAttribute& xmlAttribute : ChildNode->GetAttributes()) {
			if (xmlAttribute.GetValue() == Contribution) {
				return ChildNode;
			}
		}
	}
	for (FXmlNode* ChildNode : RootNode->GetChildrenNodes()) {
		FXmlNode* foundNode = FindXmlNode(ChildNode, Contribution);
		if (foundNode != nullptr)	return foundNode;
	}
	return nullptr;
}

FString UHermesFunctionLibrary::ReadXmlFileByContribution(const FString& Path, const FString& Contribution, bool& isSuccess) {
	TSharedPtr<FXmlFile> XmlFile = MakeShareable(new FXmlFile(Path));
	if (!XmlFile->IsValid() || !isFileExist(Path)) {
		isSuccess = false;
		return FString("");
	}
	FXmlNode* RootNode = XmlFile->GetRootNode();
	FXmlNode* foundNode = FindXmlNode(RootNode, Contribution);
	if(foundNode == nullptr)	return FString("");
	return foundNode->GetContent();
}

递归xml解析

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值