gpt4 book ai didi

java - 为什么 removeContent()、removeChild() 和 detach() 不起作用?

转载 作者:行者123 更新时间:2023-11-29 07:48:33 25 4
gpt4 key购买 nike

我有一个包含列表的 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() 抛出该异常的原因。

关于java - 为什么 removeContent()、removeChild() 和 detach() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23302037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com