Rapidxml 对一个特定节点的奇怪解析

问题描述

我找了几个小时来解决我的(简单的?)问题,但我找不到遇到这个问题的人。我正在使用最新版本的 Rapidxml(1.13)。 我目前正在尝试创建一个基于 tile 的引擎,我需要读取 tmx 文件。 我使用rapidxml 有一段时间了,到目前为止一切都很好。它能够以预期的行为完美地读取每个节点。但是我遇到了一个节点有问题。 这是我的 tmx 文件

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.5" tiledversion="1.6.0" orientation="orthogonal" renderorder="right-down" width="100" height="100" tilewidth="32" tileheight="32" infinite="0" nextlayerid="12" nextobjectid="1">
 <tileset firstgid="1" source="../../../Tile_engine/Tile_engine/sprite/[Base]BaseChip_pipo.tsx"/>
 <tileset firstgid="1065" source="../../../Tile_engine/Tile_engine/sprite/collision.tsx"/>
 <layer id="4" name="background" width="100" height="100">
  <properties>
   <property name="bg" type="bool" value="false"/>
  </properties>
  <data encoding="csv">
//I've removed the data for clearer view
</data>
 </layer>
 <layer id="6" name="object" width="100" height="100">
  <properties>
   <property name="isSolid" type="bool" value="true"/>
  </properties>
  <data encoding="csv">
//I've removed the data for clearer view
</data>
 </layer>
 <layer id="9" name="front" width="100" height="100">
  <properties>
   <property name="isSolid" type="bool" value="false"/>
  </properties>
  <data encoding="csv">
//I've removed the data for clearer view
</data>
 </layer>
 <layer id="11" name="collision" width="100" height="100">
  <data encoding="csv">
//I've removed the data for clearer view
</data>
 </layer>
</map>

为了调试,我使用了 Rapidxml 的基本读取:

 xml_document<> doc;
    xml_node<> * root_node;
    // Read the xml file into a vector
    ifstream theFile ("sprites/map_wtf.tmx");
    vector<char> buffer((istreambuf_iterator<char>(theFile)),istreambuf_iterator<char>());
    buffer.push_back('\0');
    // Parse the buffer using the xml file parsing library into doc
    doc.parse<0>(&buffer[0]);
    // Find our root node
    root_node = doc.first_node("map");

当我尝试读取(并计算)节点时:

int count_node(0);
    for(xml_node<> * child = root_node->first_node("layer"); child != nullptr; child = child->next_sibling())
        count_node++;
    cout    <<  count_node;

输出是正确的,并且给了我 4。 但是当我尝试读取 tileset 节点时,输出给了我 6.

所以我假设行为是链接到瓦片集节点 (<tileset firstgid="1" source="../../../Tile_engine/Tile_engine/sprite/[Base]BaseChip_pipo.tsx"/>) 末尾的 />。

由于嵌套的 property 节点具有相同的模式 (<property name="bg" type="bool" value="false"/>),因此我尝试了此代码

int count_node(0);
    for(xml_node<> * child = root_node->first_node("layer")->first_node("properties")->first_node("property"); child != nullptr; child = child->next_sibling())
        count_node++;
    cout    <<  count_node;

谁给了我正确的输出又名:1.

我为 doc.parse<0>(&buffer[0]) 行尝试了不同的解析选项,但没有任何效果。我还在这些测试期间阅读了缓冲区的内容,这是正确的。 我一定有错误的方式来读取文件,但我不明白为什么这个脚本读取 layer 节点和 property 节点很好,但不是 tileset

谁能帮帮我? 谢谢!

解决方法

问题是基于对结果的误解

xml_node<> * child = root_node->first_node("layer");
child = child->next_sibling();

child = child->next_sibling() 返回同一级别上的下一个节点,而不管节点名称或在 root_node->first_node("layer") 选择期间应用的任何其他约束。

然而,next_sibling 签名与 first_node 签名基本相同,因此它可用于将条件应用于要找到的下一个兄弟节点。

所以(未经测试的)方法就像

int count_node(0);
for(xml_node<> * child = root_node->first_node("tileset"); child != nullptr; child = child->next_sibling("tileset"))
    count_node++;
cout << count_node;