[jdom-interest] Remove elements

John L. Webber - Jentro AG John.Webber at jentro.com
Mon Jun 17 23:35:28 PDT 2002

Hi Dilip,

You need to remove the elements from their parent element, not from the
list. Try


or, if you want to use the element somewhere else

Element freeElement = elemSection.detach();
//Now you can insert freeElement into another element.

The API docs have plenty of information about this.



"Ladhani, Dilip" wrote:

> I am trying to remove some elements from an xml doc based on some logic. Heres a sample of what I am doing.
> List listSections = dataRs.getChildren("ABC");
> Iterator iterSections = listSections.iterator();
> while(iterSections.hasNext()){
> elemSection =  (Element) iterSections.next();
> if(elemSection.getChildTextTrim("HDG").equalsIgnoreCase("XYX"){
>         iterSections.remove();
> }
> }
> After this, when I try to print the xml, the elements which are supposed to be removed are still present!! I also tried using listSections.detach() instead of iterSections.remove(), but that didn't work either.
> What am I doing wrong????
> In general, how do I remove elements??

 Jentro AG
 John L. Webber, Software Development
 Peter-Henlein-Strasse 28, 85540 Haar/Munich, Germany
 Tel. +49 89 462 385 0     mailto:John.Webber at jentro.com 
 Fax  +49 89 462 385 29    http://www.jentro.com

More information about the jdom-interest mailing list