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解析