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

前端 未结 3 498
名媛妹妹
名媛妹妹 2021-01-15 01:23

I have a xml structure as follows:


    enabled
    

        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-15 01:45

    You'll need to do this:

    List elements = new ArrayList();
    
    while (subchilditr.hasNext()) {
        Element subchild = (Element) subchilditr.next();
        if (subchild.getText().equalsIgnoreCase(text)) {
            elements.add(subchild);
        }
    }
    
    for (Element element : elements) {
        element.getParent().removeContent(element);
    }
    

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

提交回复
热议问题