Question

I am trying to add new xml data to existing xml file. But I get this error. I try to add a new type and I got like 20 in the orginal file already.

 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(Type objType, Properties property) throws IOException, ParserConfigurationException, 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");
}

How to solve this?

Was it helpful?

Solution

Remove prop.addContent(prop)

following code block creates a clone of root element and detaches it.

org.jdom2.Document doc = new SAXBuilder().build(xmlFile);
Element root = doc.getRootElement().clone();
root.detach();

This is the reason your doc is not getting updated. Two Options:

  1. try creating a new document with the cloned root and write that to your file
  2. add the content to existing root element

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top