我有一个带有列表的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)


有人知道发生了什么吗?

提前致谢!

最佳答案

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

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

08-26 19:33