[英]JDOM - How to create an XML with internal DTD declaration?
我需要使用內部DTD聲明通過JDOM創建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();
}
}
DocType
結構能夠包含外部和內部引用。 您可以通過在doctype上調用setInternalSubset()
來添加內部子集。 輸入值必須是代表完整聲明的字符串。 JDOM不會執行doctype的內部“模型”,而是將其視為“ blob”。
請注意,您可以使用構造函數DocType(String)
創建不帶外部引用的DocType(String)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.