Returning value from deep recursion  
Author Message
Arun





PostPosted: 2005-1-26 10:23:00 Top

java-programmer, Returning value from deep recursion 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.

 
Lee Fesperman





PostPosted: 2005-1-26 11:05:00 Top

java-programmer >> Returning value from deep recursion Arun wrote:
>
> 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.

--
Lee Fesperman, FFE Software, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS (http://www.firstsql.com)