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

1 Answer 1

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.

}
Sign up to request clarification or add additional context in comments.

5 Comments

and it will create the generatedXml.xml?¿
@marcss if yo exception is thrown, yes.
and how can i know if yo exception will be thown
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.