wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Recusively removing branches Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Recusively removing branches" Watch "Recusively removing branches" New topic
Author

Recusively removing branches

justin mcsporran
Greenhorn

Joined: May 12, 2003
Posts: 15
Hi,
I have an xml document from which I'd like to select an element and then remove all of it's children, it's children's children, and so on...(ie: recursively).
I'm fine with the selecting (I'm using dom4j's XPath support for that), but I can't find a method to call on a Node to do the recursive removal. Should there be one in dom4j or will I have to use some kind of treewalk algorithm to seek out each child Node and delete it?
The only single method-based possiblity seemed to be Node's detach() method, but the dom4j javadocs don't detail whether this is recursive or not. Also, the nomenclature's a bit confusing (ie: is detaching the same as deleting?).
Anyway, many thanks in advance for any assistance from the assembled brains.
Bests,
Justin
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
I'm not clear on why just detaching the first level of children is not sufficient. Unless there are other links floating around somewhere, seems to me that the dependent nodes should be automatically GCed.
Bill
justin mcsporran
Greenhorn

Joined: May 12, 2003
Posts: 15
Hi Will,
Thanks for the reply.
I just wasn't sure if detach() would do that or not. Are you saying for sure that detach() removes all child nodes below the node being detached, and doesn't just orphan them somehow?
Cheers,
Justin
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
It doesn't matter what the internal links are within the child nodes - if the only reference connected to a running Thread has been removed, all those objects are eligible for GC.
I'm assuming you are not keeping any other links to the detached nodes.
Bill
justin mcsporran
Greenhorn

Joined: May 12, 2003
Posts: 15
Many thanks for the help.
Cheers,
Justin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Recusively removing branches