Can't create xml file using jdom

Here is my code:

import java.io.FileWriter; 
import java.io.IOException; 
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);
    doc.setRootElement(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"));
    doc.getRootElement().addContent(fichada);
    XMLOutputter xmlOutput = new XMLOutputter();
    xmlOutput.setFormat(Format.getPrettyFormat());
    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.

+3


source to share


1 answer


Remove this line:

doc.setRootElement(FICHADAS);

      



because you are setting the root element here:

Document doc = new Document(FICHADAS);

      

+3


source







All Articles