Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > XML: JDOM: removing all elements with certain attribute

Reply
Thread Tools

XML: JDOM: removing all elements with certain attribute

 
 
cyberco
Guest
Posts: n/a
 
      11-07-2007
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

 
Reply With Quote
 
 
 
 
cyberco
Guest
Posts: n/a
 
      11-07-2007
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));
================================================== ================

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-07-2007
On Wed, 07 Nov 2007 17:29:54 -0000, cyberco <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>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
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing all instances of a certain value from a list Lee Sander Python 4 03-21-2008 12:55 PM
XSD: restrict number of elements with certain attribute value Joshua Mostafa XML 4 05-11-2007 01:30 AM
removing elements invalidates only those iterators that had specifically pointed at the removed elements Alien C++ 6 09-21-2006 03:13 PM
picking value of one attribute based on a child elements attribute? XPath? vjethava@gmail.com XML 2 03-06-2006 05:19 AM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM



Advertisments