Java org.jdom2.IllegalAddException:内容已存在父级
我正在尝试将新的xml数据添加到现有的xml文件中。但我得到了这个错误。 我尝试添加一个新的类型,我在原始文件中已经得到了大约20个Java org.jdom2.IllegalAddException:内容已存在父级,java,xml,jdom,Java,Xml,Jdom,我正在尝试将新的xml数据添加到现有的xml文件中。但我得到了这个错误。 我尝试添加一个新的类型,我在原始文件中已经得到了大约20个 Exception in thread "AWT-EventQueue-0" org.jdom2.IllegalAddException: The Content already has an existing parent "type" at org.jdom2.ContentList.checkPreConditions(ContentList.ja
Exception in thread "AWT-EventQueue-0" org.jdom2.IllegalAddException: The Content already has an existing parent "type"
at org.jdom2.ContentList.checkPreConditions(ContentList.java:211)
at org.jdom2.ContentList.add(ContentList.java:244)
at org.jdom2.ContentList.add(ContentList.java:79)
at java.util.AbstractList.add(Unknown Source)
at org.jdom2.Element.addContent(Element.java:917)
at xmleditor.service.CreateNewXMLData.saveXmlToFile(CreateNewXMLData.java:64)
at xmleditor.gui.CreateNewObjectType$1.actionPerformed(CreateNewObjectType.java:405)
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.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)
public void saveXmlToFile(类型为objType,属性为Properties)
抛出IOException、ParserConfiguration异常、SAXException、,
JDOMException{
File xmlFile = new File("xmlFiles/CoreDatamodel.xml");
org.jdom2.Document doc = new SAXBuilder().build(xmlFile);
Element root = doc.getRootElement().clone();
root.detach();
root.setNamespace(Namespace.NO_NAMESPACE);
Element type = new Element("type");
Element prop = new Element("prop");
root.addContent(type);
prop.setNamespace(Namespace.NO_NAMESPACE);
type.setNamespace(Namespace.NO_NAMESPACE);
type.addContent(new Element("OBJECT_TYPE").setText(objType
.getObjectType()));
type.addContent(prop);
prop.addContent(new Element("DESCRIPTION").setText(property
.getDescription()));
prop.addContent(new Element("PARENT").setText(property.getParent()));
prop.addContent(new Element("VIRTUAL").setText(property.getVirtual()));
prop.addContent(new Element("VISIBLE").setText(property.getVisible()));
prop.addContent(new Element("PICTURE").setText(property.getPicture()));
prop.addContent(new Element("HELP").setText(property.getHelp()));
prop.addContent(new Element("MIN_NO").setText(property.getMin_no()));
prop.addContent(new Element("MAX_NO").setText(property.getMax_no()));
prop.addContent(new Element("NAME_FORMAT").setText(property
.getName_format()));
prop.addContent(prop);
XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat());
// Create a new file and write XML to it
xmlOutput.output(doc, new FileOutputStream(new File(
"xmlFiles/CoreDatamodel.xml")));
System.out.println("Wrote to file");
}
如何解决此问题?删除
prop.addContent(prop)
下面的代码块创建根元素的克隆并将其分离
org.jdom2.Document doc = new SAXBuilder().build(xmlFile);
Element root = doc.getRootElement().clone();
root.detach();
这就是您的文档未更新的原因。有两个选项:
希望这有帮助。检查'prop.addContent(prop);'这是怎么回事?我的代码中已经有了它……它消除了错误,但文件没有更新“将内容添加到现有的根元素”:root.addContent(prop);给出了与早期相同的错误Yeh,现在已经开始工作了。只需要删除事情的顺序