Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

XML: JDOM: removing all elements with certain attribute

Thread view: 
cyberco - 07 Nov 2007 17:29 GMT
Using JDOM I want to remove all elements with attribute uid="x" from
the following xml:

=======================================
<root>
   <ele>
       <sub uid="x">
           bla
       </sub>
   </ele>
   <ele uid="x">
       boe
   </ele>
</root>

=======================================

How can I do that? Iterating and removing at the same time gives me
concurrent modification exceptions.

2B
cyberco - 07 Nov 2007 22:28 GMT
Using XPath seemed to be the easiest way:

==================================================================
Document doc = new SAXBuilder().build(new StringReader(xml));
List<Element> Eles = XPath.selectNodes(doc.getRootElement(), "//
*[@uid='x']");
for (Element e: Eles) {
   e.getParentElement().removeContent(e);
}

//optionally print it
System.out.println(new XMLOutputter().outputString(doc));
==================================================================
Roedy Green - 07 Nov 2007 23:20 GMT
>How can I do that? Iterating and removing at the same time gives me
>concurrent modification exceptions.

see http://mindprod.com/jgloss/iterator.html#REMOVE
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.