1

このコードを使用して generatedXml.xml ファイルを作成しています

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"));

しかし、最後の行でエラーが発生します(Eclipseを使用しています):

この行に複数のマーカー - 未処理の例外タイプ IOException - 未処理の例外タイプ IOException

4

1 に答える 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.

}
于 2015-04-22T06:12:54.950 に答える