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