I need to create an XML by JDOM with an internal DTD declaration. By now I was creating it with an external DTD and this is the code:
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
structs are able to contain both external and internal references. You can add an internal subset by calling setInternalSubset()
on the doctype. The input value must be the string representing the full declaration. JDOM does not do an internal 'model' of the doctype - it treats it as a "blob".
Note that you can create the DocType without an external reference using the constructor DocType(String)