【发布时间】:2016-10-15 04:39:02
【问题描述】:
我需要通过 JDOM 创建一个带有内部 DTD 声明的 XML。 到目前为止,我正在使用外部 DTD 创建它,代码如下:
public static void makeFile(Element rootElement, String pathDtd){
Document documento = new Document();
DocType type = new DocType(rootElement.getName(), pathDtd);
documento.setDocType(type);
documento.setRootElement(rootElement);
XMLOutputter xmlOutputter = new XMLOutputter();
xmlOutputter.setFormat(Format.getPrettyFormat());
/* Validazione xml ottenuto */
String xmlOttenuto = xmlOutputter.outputString(documento);
SAXBuilder builder = new SAXBuilder(XMLReaders.DTDVALIDATING);
try {
Document documentoCorretto = builder.build(new StringReader(xmlOttenuto));
FileOutputStream fileOutputStream = new FileOutputStream(new File(rootElement.getName()+".xml"));
xmlOutputter.output(documentoCorretto, fileOutputStream);
} catch (FileNotFoundException e1){
System.err.println(e1);
} catch(IOException e2){
System.err.println(e2);
} catch (JDOMException e) {
e.printStackTrace();
}
}
【问题讨论】:
标签: java xml dtd internal jdom