java - 使用 Jdom 从 java 创建 xml

标签 java xml eclipse jdom jdom-2

我使用此代码来创建 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):

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

最佳答案

您的方法应该抛出 IOException,或者您必须在代码周围使用try-catch-block

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

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.

}

关于java - 使用 Jdom 从 java 创建 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789075/

相关文章:

java - 当前一次运行一个线程,想要使其并发

PHPWord 导出给出损坏的 Word 文件

c++ - eclipse c/c++ CDT 只构建一个文件

java - 是否可以使用 Glassfish 应用服务器运行 gRPC?

java - 使用 Spring CrudRepository 中其他位置的标识符和值

android - ReToucherView的onTouchListener已禁用其滚动

java - 使用 JAXB 解码嵌套 XML 元素

android - 从 Eclipse 迁移到 Android Studio,证书不一致

java.io.FileNotFoundException :/storage/sdcard/MyApp/questions. txt:打开失败:EACCES(权限被拒绝)

java - 获取用户输入并将其粘贴到其他字段java中