Can't create xml file using jdom

Here is my code:

import org.jdom2.Attribute; 
import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.output.Format; 
import org.jdom2.output.XMLOutputter;

try {
    Element FICHADAS = new Element("FICHADAS");
    Document doc = new Document(FICHADAS);
    Element fichada = new Element("fichada");
    fichada.addContent(new Element("N_Terminal").setText("XX"));
    fichada.addContent(new Element("Tarjeta").setText("XX"));
    fichada.addContent(new Element("Fecha").setText("XX"));
    fichada.addContent(new Element("Hora").setText("XX"));
    fichada.addContent(new Element("Causa").setText("XX"));
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.output(doc, new FileWriter("c:\file.xml"));
} catch(IOException e) {



I am trying to find the .xml file in C: \ but not here and I don't know why and the console will show me that: Element "FICHADAS" cannot be added as document root: Content already has an existing parent document

// NEW I thought and now I only need to add new phishads to an existing document, I don't need to create it every time I open the program.


source to share

1 answer

Remove this line:



because you are setting the root element here:

Document doc = new Document(FICHADAS);




All Articles