aspose file tools*
The moose likes Beginning Java and the fly likes Recursion makes my brain hurt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Recursion makes my brain hurt" Watch "Recursion makes my brain hurt" New topic
Author

Recursion makes my brain hurt

Andrew McLaren
Ranch Hand

Joined: May 27, 2005
Posts: 33
I am attemping to parse through an XML document, which in oversimplified form looks kinda like this:

<root>
<folder>
<table>...</table>
<folder>
<table>...
</folder>
</folder>
</root>

Since I could theoretically have any number of nested <folder> elements, I guess the only way I could do this is through recursion, yes? But I am not entirely sure I have a solid grip on how recursion works. If I have something like 5 nested calls to same method, will Java work it's way back up in the correct order, until I reach the top call?

I hope this makes some semblance of sense.

Thanks!

Andrew
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Yes, such recursive calls will work fine, and are used by many programs. There is a limit to the depth of call nesting, according to the Java stack size. However, it is a limit that very few real-life programs will reach and not something to worry about as a beginner. It is certainly unlikely that your XML document would be sufficiently nested to cause such problems.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
Originally posted by Andrew McLaren:

Since I could theoretically have any number of nested <folder> elements, I guess the only way I could do this is through recursion, yes? But I am not entirely sure I have a solid grip on how recursion works. If I have something like 5 nested calls to same method, will Java work it's way back up in the correct order, until I reach the top call?


Yes and yes.

Of course you have to careful with what you do at each level of recursion. If you'd like to take a stab at some code and post it here (using CODE tags, naturally), I'm sure someone would be happy to give you some help with any problems you're having.
[ December 01, 2005: Message edited by: Ryan McGuire ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Andrew --

Parsing XML is complicated by all sorts of little ugly details, which is why there are a number of standard APIs for XML parsers, and a multitude of available XML parsers that do the whole job for you. For an extremely, extremely simple document, you might be able to justify doing it by hand, but given what you describe, I hope you're instead using an existing parser, and you're simply asking us about recursively examining the parser's output (a DOM tree, for example.)

See here if you're not sure what I'm talking about.


[Jess in Action][AskingGoodQuestions]
Andrew McLaren
Ranch Hand

Joined: May 27, 2005
Posts: 33
Thanks for all the replies. This is a pretty big lead forward for me, so we'll see how it goes.

And yes, I am using dom4j. I couldn't even imagine trying to write my own parser.

Peace

Andrew
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your recursion will probably look something like:

Draw out an XML doc with a couple nested folders, follow this through and write down what it would print. I had to do recursion on paper this way many times before it came naturally. It takes a little effort to remember just where to "pop back" when you return from the nested call.
[ December 01, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Andrew McLaren
Ranch Hand

Joined: May 27, 2005
Posts: 33
Originally posted by Stan James:
Your recursion will probably look something like:

Draw out an XML doc with a couple nested folders, follow this through and write down what it would print. I had to do recursion on paper this way many times before it came naturally. It takes a little effort to remember just where to "pop back" when you return from the nested call.

[ December 01, 2005: Message edited by: Stan James ]



Here's what I came up with (liberally pillaged from the dom4j cookbook):



It definitely took me a few stabs to get it going, but it looks to be working so far!
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Very cool! Keep it fun!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recursion makes my brain hurt