recursive function kills me

U

Uwe Grawert

I have the following recursive function:

string find_value_by_key (xmlDocPtr doc,
xmlNodePtr root_node,
const string& key)
{
while(root_node != NULL)
{
if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
cout << root_node->name << ": " << xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1);
return string((char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1));
}

if(root_node->children)
find_value_by_key(doc, root_node->children, key);

root_node = root_node->next;
}

return string("Nothing!");
}

I can find the element and it prints the value. but the return is
always "Nothing!".
i think i make a classical mistake, but have no idea why the function does
not return the value of the element???
 
L

Lattamore Osburn

It looks like the return syntax might be wrong.

return string((char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1));

This creates a temporary variable of type string which is destroyed when
scope is gone.
Try returning just the cast itself

return (char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1);
 
U

Uwe Grawert

Am Mon, 20 Mar 2006 18:41:32 -0800 schrieb Lattamore Osburn:
It looks like the return syntax might be wrong.

return string((char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1));

This creates a temporary variable of type string which is destroyed when
scope is gone.
Try returning just the cast itself

return (char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1);

But then it wouldn`t return "Nothing" !!! wouldn`t it?
 
L

Larry I Smith

Uwe said:
Am Mon, 20 Mar 2006 18:41:32 -0800 schrieb Lattamore Osburn:


But then it wouldn`t return "Nothing" !!! wouldn`t it?

Are you using the Microsoft COM XML interface?
If you are, aren't those 'xmlStrings' of type BSTR
(int 16 wide chars)? If they are, then the xmlStrcmp()
call will probably never match to the 8-bit char's used by
the C++ 'string' class - and returned by key.c_str().

Regards,
Larry
 
G

GB

Uwe said:
I have the following recursive function:

string find_value_by_key (xmlDocPtr doc,
xmlNodePtr root_node,
const string& key)
{
while(root_node != NULL)
{
if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
cout << root_node->name << ": " << xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1);
return string((char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1));
}

if(root_node->children)
find_value_by_key(doc, root_node->children, key);

It looks like you are not using the value returned by this call to
find_value_by_key. You should probably be comparing it to "Nothing!" and
returning it if it is different.
root_node = root_node->next;
}

return string("Nothing!");
}

I can find the element and it prints the value. but the return is
always "Nothing!".
i think i make a classical mistake, but have no idea why the function does
not return the value of the element???

Gregg
 
J

JE

Uwe said:
I have the following recursive function:

string find_value_by_key (xmlDocPtr doc,
xmlNodePtr root_node,
const string& key)
{
while(root_node != NULL)
{
if(! xmlStrcmp( root_node->name, (xmlChar*)key.c_str() ) ) {
cout << root_node->name << ": " << xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1);
return string((char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1));
}

if(root_node->children)
{
std:: string halfBakedRecursionReturn =
find_value_by_key(doc, root_node->children, key);
if(halfBakedRecursionReturn != "Nothing!")
{
return halfBakedRecursionReturn;
}
}
 
R

Richard Herring

Lattamore Osburn said:
It looks like the return syntax might be wrong.

return string((char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1));

This creates a temporary variable of type string which is destroyed when
scope is gone.

Irrelevant. The function returns by value, not reference, so what it
returns is a *copy* of the temporary.
Try returning just the cast itself

return (char*) xmlNodeListGetString(doc,
root_node->xmlChildrenNode, 1);

.... which constructs a temporary of the function's return type (i.e.
string) and returns a copy of it. Not a lot of difference.

The problem is elsewhere.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top