mad_programmer mad_programmer - 1 year ago 101
Java Question

How to remove a child from from a node using jdom in java?

I have a xml structure as follows:

<rurl modify="0" children="yes" index="8" name="R-URL">
<rurl-link priority="3">http</rurl-link>
<rurl-link priority="5">http://localhost:80</rurl-link>
<rurl-link priority="4">abc</rurl-link>
<rurl-link priority="3">b</rurl-link>
<rurl-link priority="2">a</rurl-link>
<rurl-link priority="1">newlinkkkkkkk</rurl-link>

Now, I want to remove a child node, where text is equal to http. currently I am using this code:

Element subchild = (Element);
if (subchild.getText().equalsIgnoreCase(text)) {
message = subchild.getText();
update = "Success";

But it is not completely removing the sub element from xml file. It leaves me with


Any suggestions?

Answer Source

You'll need to do this:

List<Element> elements = new ArrayList<Element>();

while (subchilditr.hasNext()) {
    Element subchild = (Element);
    if (subchild.getText().equalsIgnoreCase(text)) {

for (Element element : elements) {

If you try to remove an element inside of the loop you'll get a ConcurrentModificationException.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download