如何使用tinyxml2从XML加载父实体和子实体
How to load parent and child entities from XML using tinyxml2?
我正在使用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
- std::原子加载和存储都需要吗
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 为什么加载SDF会导致Mobilizer创建闭环错误
- C++atioglxx.pdb未加载错误glBufferData OpenGL
- 如何使用tinyxml2从XML加载父实体和子实体
- 如何在C++中使用pybind11加载一个pickle python列表
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 当我尝试加载内核模块时,如何修复C++中的这个 malloc() 错误?
- 树莓上的 Libtorch 无法加载 pt 文件,但在 ubuntu 上工作
- Python ctypes:不会按预期加载 dll
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 保存/加载大量短数组到二进制文件
- 使用 SDL2 和SDL_image C++加载图像
- 无法使用加载库卸载注入的 dll (C++)
- 是否有原子加载非原子值的函数?
- 将 bmp 文件加载到 TSpeedButton 中
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常