C ++ RapidXML获得同类型的兄弟? [英] C++ RapidXML get sibling of the same type?

查看:170
本文介绍了C ++ RapidXML获得同类型的兄弟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在RapidXML中,我试图循环遍历我的文件以从一些 tileset 节点获取数据:

So, in RapidXML, I'm trying to loop through my file to get the data from some tileset nodes:

rapidxml::xml_node<> *root_node = doc.first_node("map");
for(rapidxml::xml_node<> *tileset = root_node->first_node("tileset");
    tileset != 0; tileset = tileset->next_sibling("tileset"))
{
    // Iteration stuff...

有什么问题?那么,在RapidXML中, next_sibling()函数可选匹配以下名称:

You're probably saying, what's the problem? Well, in RapidXML, the next_sibling() function optionally matches the name:

xml_node<Ch>* next_sibling(const Ch *name=0, std::size_t name_size=0, bool
   case_sensitive=true) const;

Gets next sibling node, optionally matching node name. Behaviour is undefined 
   if node has no parent. Use parent() to test if node has a parent.

因此,如果没有找到带有名称的节点,它将只返回下一个兄弟节点。这是我的程序中的一个问题,我只是简单地不想要额外的迭代。我认为这是愚蠢的,但无论如何。是否有方法让遍历我的 tileset 节点?

Hence, if a node is not found with the name, it'll just return the next sibling regardless. This is a problem in my program, and I just plain don't want the extra iteration. I think this is stupid, but whatever. Is there a way to make it ONLY iterate through my tileset nodes?

推荐答案

可选匹配节点名称 - 在参数中是可选的。如果传递一个名称字符串,并且找不到,您将获得一个零返回值。

"optionally matching node name" - As in the parameter is optional. If you pass a name string, and it is not found you will get a return value of zero.

xml_node<Ch> *next_sibling(const Ch *name = 0, std::size_t name_size = 0, bool case_sensitive = true) const
{
    assert(this->m_parent);     // Cannot query for siblings if node has no parent
    if (name)
    {
        if (name_size == 0)
            name_size = internal::measure(name);
        for (xml_node<Ch> *sibling = m_next_sibling; sibling; sibling = sibling->m_next_sibling)
            if (internal::compare(sibling->name(), sibling->name_size(), name, name_size, case_sensitive))
                return sibling;
        return 0;
    }
    else
        return m_next_sibling;
}

这篇关于C ++ RapidXML获得同类型的兄弟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆