Returning value from deep recursion

A

Arun

I have the following code

***********************************
/**
* Recurses through schema finding the first instance of the
element name
* @param schemaElement - current element being compared from
schema
* @param checkElement - element to be checked against i.e. the
node selected in the gui
* @return the element in schema which matches the checkElement
*/
private Element findElementNameMatch(Element schemaElement, Element
checkElement)
{
// Get list of children
List listOfChildren = schemaElement.getChildren();

// If element has children
if(listOfChildren.size() != 0)
{
// Recurse through all elements under this one
for(int x=0; x < listOfChildren.size(); x++)
{
// Get child element
Element childElement = (Element) listOfChildren.get(x);

if(childElement.getName() == checkElement.getName())
{
return(childElement);
}
findElementNameMatch(childElement,checkElement);
}
}

return(null);
}
***********************************


The method is trying to find an element in an xml document that matches
a given name.

Can anyone give me tips on how to go about returning a value from deep
recursion to the first recursion of that particular method, so it can
return that value to the method that called it.

And also quite importantly how to stop any recursions after a match has
been found. EG a target element is found and returned, but how can i
stop the recursion going on to check elements under this element.

So far i have only achieved the two by creating a global variable and
flag, the variable is initialised when the match is found, and flag is
set to true so no more recursions are done afterwards.
 
L

Lee Fesperman

Arun said:
I have the following code

***********************************
/**
* Recurses through schema finding the first instance of the
element name
* @param schemaElement - current element being compared from
schema
* @param checkElement - element to be checked against i.e. the
node selected in the gui
* @return the element in schema which matches the checkElement
*/
private Element findElementNameMatch(Element schemaElement, Element
checkElement)
{
// Get list of children
List listOfChildren = schemaElement.getChildren();

// If element has children
if(listOfChildren.size() != 0)
{
// Recurse through all elements under this one
for(int x=0; x < listOfChildren.size(); x++)
{
// Get child element
Element childElement = (Element) listOfChildren.get(x);

if(childElement.getName() == checkElement.getName())
{
return(childElement);
}
findElementNameMatch(childElement,checkElement);
}
}

return(null);
}
***********************************

The method is trying to find an element in an xml document that matches
a given name.

Can anyone give me tips on how to go about returning a value from deep
recursion to the first recursion of that particular method, so it can
return that value to the method that called it.

And also quite importantly how to stop any recursions after a match has
been found. EG a target element is found and returned, but how can i
stop the recursion going on to check elements under this element.

So far i have only achieved the two by creating a global variable and
flag, the variable is initialised when the match is found, and flag is
set to true so no more recursions are done afterwards.

You need to grab the result of your inner call and check for null. If it isn't null,
simply return that result. BTW, you're currently ignoring inner results.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,087
Latest member
JeremyMedl

Latest Threads

Top