为什么removeContent(),removeChild()和detach()不起作用?

时间:2014-04-25 20:05:12

标签: java xml removechild

我有一个带有列表的XML文件,其中每个元素只有一个属性,我试图删除一个特定的元素,但它不起作用。我的删除Java代码是:

SAXBuilder builder = new SAXBuilder();
        Document readDoc;

        try {
            readDoc = builder.build(new File(INACTIVE_FILE));

            List<Element> inactiveDocList = readDoc.getRootElement().getChild("inactiveDoctorList").getChildren();

            for(Element e: inactiveDocList){
                if(e.getAttributeValue("inactiveDoctorID").equals("1")){
                    e.removeContent();      
                }
            }
            XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
            xmlOutput.output(readDoc.getRootElement(), new FileOutputStream(new File(INACTIVE_FILE)));      
        } 
        catch (JDOMException | IOException e) { 
            e.printStackTrace();
        }

我的XML是:

<root>
  <inactiveDoctorList>
    <doctor inactiveDoctorID="1" />
    <doctor inactiveDoctorID="2" />
    <doctor inactiveDoctorID="4" />
    <doctor inactiveDoctorID="16" />
    <doctor inactiveDoctorID="3" />
    <doctor inactiveDoctorID="9" />
    <doctor inactiveDoctorID="13" />
    <doctor inactiveDoctorID="14" />
    <doctor inactiveDoctorID="11" />
    <doctor inactiveDoctorID="6" />
  </inactiveDoctorList>
</root>

当我执行e.removeContent()或e.removeChild(“doctor”)时,没有任何反应,当我尝试e.detach()时,我收到此错误:

Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException: The ContentList supporting the FilterList this iterator is processing has been modified by something other than this Iterator.
    at org.jdom2.ContentList$FilterListIterator.checkConcurrent(ContentList.java:1309)
    at org.jdom2.ContentList$FilterListIterator.next(ContentList.java:1357)
    at org.jdom2.ContentList$FilterListIterator.next(ContentList.java:1260)
    at edu.miami.cis324.swing.jfarache.RestorePatDoc.reactivate(RestorePatDoc.java:61)
    at edu.miami.cis324.swing.jfarache.MainFrame$13.actionPerformed(MainFrame.java:741)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

任何人都知道发生了什么事?

提前致谢!

2 个答案:

答案 0 :(得分:5)

您无法从for-each循环内部更改基础集合,而是需要while循环或实际迭代器。

它不适用于for-each循环,因为删除(或添加)元素意味着循环不知道下一个要显示的元素。我不知道removeContent()或removeChild(),但这就是detach()抛出异常的原因。

答案 1 :(得分:0)

您的变量e的范围在for-each循环中。修改它不会修改readDoc

编辑:划伤,我想我错了。无法赢得所有人。