JDOM - 如何创建带有内部 DTD 声明的 XML?

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 结构能够包含外部和内部引用。您可以通过在文档类型上调用 setInternalSubset() 来添加内部子集。输入值必须是表示完整声明的字符串。 JDOM 不执行文档类型的内部 'model' - 它将其视为 "blob".

请注意,您可以使用构造函数在没有外部引用的情况下创建 DocType DocType(String)