1

I'm using this code to create the generatedXml.xml file

Element FICHADAS = new Element("FICHADAS");
Document doc = new Document(FICHADAS);
doc.setRootElement(FICHADAS);
Element fichada = new Element("fichada");
fichada.addContent(new lement("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("generatedXml.xml"));

But I get an error in the last line (I'm using eclipse):

Multiple markers at this line - Unhandled exception type IOException - Unhandled exception type IOException

NorthCat
  • 9,643
  • 16
  • 47
  • 50
marcss
  • 253
  • 2
  • 14

1 Answers1

1

Your method should be throw the IOException or you have to use a try-catch-block arround your code.

public void myMethod() throws IOException {
 ...
}

or

try{
Element FICHADAS = new Element("FICHADAS");
Document doc = new Document(FICHADAS);
doc.setRootElement(FICHADAS);
Element fichada = new Element("fichada");
fichada.addContent(new lement("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("generatedXml.xml"));
} catch(IOException){
  // handle the exception.

}
Jens
  • 67,715
  • 15
  • 98
  • 113
  • and it will create the generatedXml.xml?¿ – marcss Apr 22 '15 at 06:17
  • @marcss if yo exception is thrown, yes. – Jens Apr 22 '15 at 06:18
  • and how can i know if yo exception will be thown – marcss Apr 22 '15 at 06:20
  • If you will add code in the catch block like `System.out.println("Exception "+e.getMessage())`, you will see it in the console if a exception is thrown. – Jens Apr 22 '15 at 06:22
  • I solved this part but now the console show me that: Exception in thread "Timer-0" org.jdom2.IllegalAddException: The element "FICHADAS" could not be added as the root of the document: The Content already has an existing parent document – marcss Apr 22 '15 at 06:49