递归问题在解析与RapidXML / C ++类指针副作用 [英] recursion problem in parsing with RapidXML/C++ class pointers side-effect

查看:144
本文介绍了递归问题在解析与RapidXML / C ++类指针副作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分享这个奇怪但有趣的情况,我最近偶然尝试使用RapidXML进行C ++中的XML解析。

I want to share this odd but interesting situation I stumbled upon recently while trying to use RapidXML for XML parsing in C++.

我想写一个递归函数搜索并返回给定节点的子节点中的特定节点。我的第一个尝试是:

I wanted to write a recursive function to search and return a particular node among the children of a given node. My first try was:

xml_node<>* get_child(xml_node<> *inputNode, string sNodeFilter)
{
    // cycles every child
    for (xml_node<> *nodeChild = inputNode->first_node(); nodeChild; nodeChild = nodeChild->next_sibling())
    {
        if (nodeChild->name() == sNodeFilter)
        {
            cout << "node name " << nodeChild->name() << "\n";
            cout << "nodeChild " << nodeChild << endl;
            // returns the desired child
            return nodeChild;
        }
        get_child(nodeChild, sNodeFilter);
    }
}

但是如果你搜索一个在你的XML文件中嵌套得更深的节点,则发现该节点(我看到cout的),但是在return语句之后,for循环似乎运行一个(或某些)更多的时间(可能是因为调用

It happened to work correctly only with the first children, but if you search for a node that is nested deeper in your XML file, the node is found (I see the cout's) but after the return statement the for cycle seems to run one (or some) more time (probably because of the call stack of the recursion), then exit and the pointer gets lost.

所以我试着用一个临时变量来修复它:

So I tried to fix it with a temporary variable, this way:

xml_node<>* get_child(xml_node<> *inputNode, string sNodeFilter)
{
    xml_node<> *outputNode;
    // cycles every child
    for (xml_node<> *nodeChild = inputNode->first_node(); nodeChild; nodeChild = nodeChild->next_sibling())
    {
        if (nodeChild->name() == sNodeFilter)
        {
            cout << "node name " << nodeChild->name() << "\n";
            cout << "nodeChild " << nodeChild << endl;
            outputNode = nodeChild;
            cout << "outputNode " << outputNode << endl;
            // returns the desired child
            return outputNode;
        }
        get_child(nodeChild, sNodeFilter);
    }
}

但没有任何变化。

不幸的是,RapidXML中的节点是类指针,因此在这种情况下,副作用会阻止我提取正确的结果。

Unfortunately nodes in RapidXML are class pointers, so in this situation the side-effect prevents me from pulling out the correct result.

任何人发现这种情况,或者以另一种方式解决了这个问题?

Anyone has found this situation, or has solved this problem in another way?

推荐答案

孩子通过递归,返回。如果没有找到子元素,请返回0

When you find a child by recursing, return it. If you don't find a child, return 0

xml_node<>* get_child(xml_node<> *inputNode, string sNodeFilter)
{
    // cycles every child
    for (xml_node<> *nodeChild = inputNode->first_node(); nodeChild; nodeChild = nodeChild->next_sibling())
    {
        if (nodeChild->name() == sNodeFilter)
        {
            cout << "node name " << nodeChild->name() << "\n";
            cout << "nodeChild " << nodeChild << endl;
            // returns the desired child
            return nodeChild;
        }
        xml_node<> * x = get_child(nodeChild, sNodeFilter);
        if (x) 
          return x;
    }
    return 0;
}

这篇关于递归问题在解析与RapidXML / C ++类指针副作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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