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

  • SEARCH
  • Home
  • 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

I want to add share this on Facebook to a page. I've got this
I want to use a temp directory that will be unique to this build.
My application generates data and i want to share this data, like transfer to
I want to share an object between my servlets and my webservice (JAX-WS) by
We want to share user validation configuration between a Java validation class (for sanity
I've a small project that I want to share with a few others on
Say I have the following interface that I want to share between my server
Looking for good project methodologies.Anyone got some good reference links..want to share.
I have a single Rails 2.2.2 app that I want to 'share' with multiple
I spent hours researching the problem, and just want to share a solution in

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.