Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not create the 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 try to find the file.xml in C:\ but is not here and I don't know why, and the console show me that: The element "FICHADAS" could not be added as the root of the document: The Content already has an existing parent document

//NEW I was thinking, and now i only need to add the new fichadas to the existing document, not need to create it every time that i opened the program.

like image 583
marcss Avatar asked Jan 31 '26 01:01

marcss


1 Answers

Remove this line:

doc.setRootElement(FICHADAS);

because you set the root element here:

Document doc = new Document(FICHADAS);
like image 76
Jens Avatar answered Feb 01 '26 15:02

Jens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!