Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4108824
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:42:37+00:00 2026-05-20T21:42:37+00:00

I want to share this odd but interesting situation I stumbled upon recently while

  • 0

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);
    }
}

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);
    }
}

But nothing changed..

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:42:38+00:00Added an answer on May 20, 2026 at 9:42 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for the odd question but this is one that makes me want to
I'm building a music events website and want to have a 'share this event'
This is the URL I want to share: http://mydomain.com/#url=http://stackoverflow.com Inside my site, I do
I want to use cloudmade gem but it is not stored in rubyforge so
class A implements Callable{ List result // want to share this List amongst thread
This is a little odd but I need to know all subclasses of a
My application generates data and i want to share this data, like transfer to
I want to add share this on Facebook to a page. I've got this
I want to share a dictionary between my processes as follows: def f(y,x): y[x]=[x*x]
I want to share(and give control of) my Ubuntu 12.04 desktop to my brother(using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.