如何使用tinyxml2从XML加载父实体和子实体

How to load parent and child entities from XML using tinyxml2?

本文关键字:实体 加载 XML 何使用 tinyxml2      更新时间:2023-10-16

我正在使用XML为我正在开发的游戏引擎存储级别文件。我最近添加了实体父子关系,每个实体都有一个指向其子级的指针向量,以及一个指向其父级的指针。每个实体都有一个方法,该方法使用一个指针来添加父对象,该方法还将其添加到父对象的子对象向量中。还有一种添加子对象的方法,它做同样的事情,但相反。问题:我不知道如何从XML加载带有子项的实体。以下是我想要的典型场景文件的样子(我已经有了加载实体及其组件的代码,我只是不确定如何加载父子关系(

<scene>
<entity name="Parent Entity">
<transform posx="500" posy="100" scalex="1" scaley="1" rotation="0"/>
<sprite image="Assets/Sprites/Avatar2.png"/>
<entity name="Child Entity">
<transform posx="0" posy="0" scalex="1" scaley="1" rotation="0"/>
<sprite image="crimson-logo.png"/>
</entity>
</entity>
</scene>

因此,本质上,我需要做的是循环遍历所有实体,并使用实体管理器的createEntity方法(返回指向新实体的指针(创建它们,检查它们在XML中是否有子实体,然后如果有,则用指向子实体的指针调用父实体的addChild方法,或用指向父实体的指针访问当前实体的addParent方法。我想我需要使用递归函数,但我该如何编写这样的函数呢?

这是一种使用递归的经典任务类型。递归可能很棘手,但在处理tinyxml时尤其如此,tinyxml不是最友好的API。

第1步:find助手

让我们变得更友好。在所有级别上,我们都希望访问所有";实体";元素。让我们制作一个方便的助手来使用TinyXML来获得这些:

auto find(TiXmlElement const* node, char const* name) {
std::vector<TiXmlElement const*> found;
for (
auto el = node->FirstChildElement(name);
el;
el = el->NextSiblingElement(name)
)
{
found.push_back(el);
}
return found;
}

到目前为止还不错,现在我们可以更容易地查询具有特定名称的节点。

第2步:分析单个实体

让我们暂时忘记那个助手,假设我们已经有了一个";实体";已选择节点。现在,我们想将其解析为一个实体(名称、精灵等(,并返回一个新创建的实体:

Entity* parse_entity(TiXmlElement const* node) {
Entity* entity = g_manager.createEntity(node->Attribute("name"));
// todo transforms, sprite info etc.
return entity;
}

第3步:建立树

这听起来像是复杂的部分。但实际上,使用上面的find助手,我们所需要的只是:

void parse_sub_entities(TiXmlElement const* node, Entity* parent = nullptr) {
for (auto el : find(node, "entity")) {
auto entity = parse_entity(el);
if (parent && entity) {
entity->addParent(parent);
parent->addChild(entity);
}
parse_sub_entities(el, entity);
}
}

所有子节点都使用步骤2中的parse_entity进行解析,并且只有当我们有一个父节点时,我们才会添加关系。

为了完成这一切,我们递归到子实体中,这次将当前实体作为父实体传递。

完整演示

**不在Coliru上直播

#include <tinyxml.h>
#include <vector>
#include <list>
#include <iostream>
#include <iomanip>
namespace { // helper functions for XML searching
auto find(TiXmlElement const* node, char const* name) {
std::vector<TiXmlElement const*> found;
for (
auto el = node->FirstChildElement(name);
el;
el = el->NextSiblingElement(name)
)
{
found.push_back(el);
}
return found;
}
}
auto scene = R"(<scene>
<entity name="Parent Entity">
<transform posx="500" posy="100" scalex="1" scaley="1" rotation="0"/>
<sprite image="Assets/Sprites/Avatar2.png"/>
<entity name="Child Entity">
<transform posx="0" posy="0" scalex="1" scaley="1" rotation="0"/>
<sprite image="crimson-logo.png"/>
</entity>
</entity>
</scene>)";
struct Entity {
std::string _name;
Entity* _parent = nullptr;
std::vector<Entity*> _children;
explicit Entity(std::string name = "unnamed") : _name(std::move(name)) {}
void addChild(Entity* e) { _children.push_back(e); }
void addParent(Entity* e) {
assert(!_parent || _parent == e);
_parent = e;
}
};
struct Mgr {
std::list<Entity> _entities;
Entity* createEntity(std::string name) {
return &_entities.emplace_back(name);
};
} g_manager;
Entity* parse_entity(TiXmlElement const* node) {
Entity* entity = g_manager.createEntity(node->Attribute("name"));
// todo transforms, sprite info etc.
return entity;
}
void parse_sub_entities(TiXmlElement const* node, Entity* parent = nullptr) {
for (auto el : find(node, "entity")) {
auto entity = parse_entity(el);
if (parent && entity) {
entity->addParent(parent);
parent->addChild(entity);
}
parse_sub_entities(el, entity);
}
}
int main() {
TiXmlDocument doc;
doc.Parse(scene);
parse_sub_entities(doc.RootElement());
std::cout << "Manager has " << g_manager._entities.size() << " entitiesn";
for (auto& e: g_manager._entities) {
std::cout << "==== Entity: " << std::quoted(e._name) << "n";
if (e._parent)
std::cout << " - has parent " << std::quoted(e._parent->_name) << "n";
for (auto child : e._children)
std::cout << " - has child " << std::quoted(child->_name) << "n";
}
}

打印:

Manager has 2 entities
==== Entity: "Parent Entity"
- has child "Child Entity"
==== Entity: "Child Entity"
- has parent "Parent Entity"

我知道的任何在线编译器上都没有安装tinyxml