I have an XML file with a list where each element only has an attribute, and I'm trying to delete a specific element, but it doesn't work. My Java code for deleting is:
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();
}
And my XML is:
<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>
When I do e.removeContent() or e.removeChild("doctor") nothing happens, and when I try e.detach() I get this error:
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)
Anyone know what's going on?
Thanks in advance!
<doctor inactiveDoctorID="1" />
?