UE4 Xml读写

UE4自带一个XmlParser,可以很方便的实现Xml的读写。

1,在PublicDependencyModuleNames.AddRange中添加XmlParser。

2,include XmlParser.h

读写操作封装在了xmlobject  需要根据需求增加 修改

xmlobject.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include <map>

/**
 * 
 */
struct NodeStruct
{
    FString tag;
    FString content;

    NodeStruct(FString Tag,FString Content)
    {
        tag = Tag;
        content = Content;
    }
};

class TESTJIGOU_API XmlFileObject
{
public:
    XmlFileObject(const FString &filePath, const FString &fileName,int NodeCount = 0,...);
    ~XmlFileObject();

public:
    class FXmlFile* m_File;
    class FXmlNode* m_RootNode;
    FString m_FilePath;
    FString m_FileName;
    bool loadFileSuccess;

public:
    bool SetNode(const FString &tag, const FString &content);
    bool SetNode(const FString &tag, int content);
    bool SetNode(const FString &tag, float content);

    bool AddChild(const FString &ParentNodeTag,const FString& ChildNodeTag,const FString &ChildNodeContent);
    bool AddChild(FXmlNode* ParentNode, const FString& ChildNodeTag, const FString& ChildNodeContent);
    

    FXmlNode* GetNode(const FString& tag,const FString &content);


    FXmlNode* GetChildNode(FXmlNode* TargetNode, const FString& ChildTag);
    FXmlNode* GetChildNode(FXmlNode* TargetNode, const FString& ChildTag, const FString& ChildContent);

    const TCHAR* GetChildNodeContent(FXmlNode* TargetNode, const FString& ChildTag);

    const TCHAR* GetNodeContent(const FString &tag);

private:
    void Save();
};
xmlobject.h

xmlobject.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "XmlFileObject.h"
#include "XmlParser.h"
#include "Engine.h"
#include "stdarg.h"

XmlFileObject::XmlFileObject(const FString &filePath, const FString &fileName,int NodeCount, ...) : m_FileName(fileName), m_FilePath(filePath)
{
    m_File = new FXmlFile(filePath + fileName);
    
    if (m_File == nullptr)
    {
        GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("打开Xml文件失败啦"));
        loadFileSuccess = false;
    }
    else
    {
        m_RootNode = m_File->GetRootNode();

        if (m_RootNode == nullptr)
        {
            GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("获取根节点失败啦"));

            const FString XmlRootNodeContent = "<RootNode>\n</RootNode>";
            m_File = new FXmlFile(XmlRootNodeContent, EConstructMethod::ConstructFromBuffer);

            if (m_File == nullptr)
            {
                GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("创建Xml文件失败啦"));
                loadFileSuccess = false;
            }
            else
            {
                GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("创建Xml文件成功啦"));
                m_RootNode = m_File->GetRootNode();

                if (NodeCount == 0)
                {
                    GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("没有创建默认Xml节点"));
                    loadFileSuccess = true;
                }
                va_list arg_ptr;
                va_start(arg_ptr, NodeCount);

                for (int i = 0; i < NodeCount; i++)
                {
                    auto node = va_arg(arg_ptr, NodeStruct);
                    SetNode(node.tag, node.content);
                }
                va_end(arg_ptr);
                loadFileSuccess = true;
                this->Save();
            }

        }
        else
        {
            loadFileSuccess = true;
            this->Save();
            GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("打开Xml文件成功啦"));
        }
    }
}

XmlFileObject::~XmlFileObject()
{
}

void XmlFileObject::Save()
{
    m_File->Save(m_FilePath + m_FileName);
}


bool XmlFileObject::SetNode(const FString &tag, const FString &content)
{
    FXmlNode* FindNode = m_RootNode->FindChildNode(tag);

    if (FindNode == nullptr)
    {
        GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("不存在该Node"));

        m_RootNode->AppendChildNode(tag, content);

        if (m_RootNode->FindChildNode(tag) == nullptr)
        {
            GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("创建Node失败"));
            return false;
        }
        else
        {
            GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("创建Node成功"));
            this->Save();
            return true;
        }
    }
    else
    {
        FindNode->SetContent(content);
        GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("设置Node成功"));
        this->Save();
        return true;
    }
}

bool XmlFileObject::SetNode(const FString &tag, int content)
{
    return this->SetNode(tag, FString::FromInt(content));
}

bool XmlFileObject::SetNode(const FString &tag, float content)
{
    return this->SetNode(tag, FString::SanitizeFloat(content));
}

bool XmlFileObject::AddChild(const FString &ParentNodeTag, const FString& ChildNodeTag, const FString &ChildNodeContent)
{
    auto ParentNode = m_RootNode->FindChildNode(ParentNodeTag);

    return this->AddChild(ParentNode, ChildNodeTag, ChildNodeContent);
}

bool XmlFileObject::AddChild(FXmlNode* ParentNode, const FString& ChildNodeTag, const FString& ChildNodeContent)
{
    if (ParentNode == nullptr)
    {
        GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("该节点不存在,无法给该节点添加子节点"));
        return false;
    }
    else
    {
        ParentNode->AppendChildNode(ChildNodeTag, ChildNodeContent);
        if (ParentNode->FindChildNode(ChildNodeTag) == nullptr)
        {
            GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("子节点创建失败"));
            return false;
        }
        else
        {
            GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("子节点创建成功"));
            this->Save();
            return true;
        }
    }
}

FXmlNode* XmlFileObject::GetNode(const FString& tag, const FString &content)
{
//     auto FindNodeList = m_RootNode->GetChildrenNodes();
// 
//     for (auto node : FindNodeList)
//     {
//         if (node->GetContent().Equals(content) && node->GetTag().Equals(tag))
//         {
//             return node;
//         }
//     }
//     return nullptr;

    return this->GetChildNode(m_RootNode, tag, content);
}

FXmlNode* XmlFileObject::GetChildNode(FXmlNode* TargetNode, const FString& ChildTag, const FString& ChildContent)
{
    auto FindNodeList = TargetNode->GetChildrenNodes();

    for (auto node : FindNodeList)
    {
        if (node->GetContent().Equals(ChildContent) && node->GetTag().Equals(ChildTag))
        {
            return node;
        }
    }
    return nullptr;
}

FXmlNode* XmlFileObject::GetChildNode(FXmlNode* TargetNode, const FString& ChildTag)
{
    auto FindNodeList = TargetNode->GetChildrenNodes();

    for (auto node : FindNodeList)
    {
        if (node->GetTag().Equals(ChildTag))
        {
            return node;
        }
    }
    return nullptr;
}

const TCHAR* XmlFileObject::GetChildNodeContent(FXmlNode* TargetNode, const FString& ChildTag)
{
    const TCHAR* result = *(GetChildNode(TargetNode, ChildTag)->GetContent());
    return result;
}

const TCHAR* XmlFileObject::GetNodeContent(const FString &tag)
{
    FXmlNode* findNode = m_RootNode->FindChildNode(tag);
    if (findNode == nullptr)
    {
        GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("查找该Node失败"));
        //错误代码2222
        const TCHAR* tempChar = *FString("2222");
        return tempChar;
    }
    else
    {
        const TCHAR* tempChar = *(findNode->GetContent());
        return tempChar;
    }
}
xmlobject.cpp

 

posted @ 2017-10-13 15:02  Litmin  阅读(2597)  评论(0编辑  收藏  举报