【问题标题】:JDOM - How to create an XML with internal DTD declaration?JDOM - 如何使用内部 DTD 声明创建 XML?
【发布时间】: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


    【解决方案1】:

    DocType 结构可以包含外部和内部引用。您可以通过在文档类型上调用 setInternalSubset() 来添加内部子集。输入值必须是表示完整声明的字符串。 JDOM 不做 doctype 的内部“模型”——它把它当作一个“blob”。

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

    【讨论】:

    • 这样它只写: "" 没有完整的 dtd 声明。如果我想让它写完整的 dtd 怎么办?
    • 像这样: ]> XML 教程quackit.com/xml/tutorial</url> HTML 教程quackit.com/html/tutorial</url>
    • 然后拨打type.setInternalSubset("&lt;!ELEMENT tutorials (tutorial)+&gt; &lt;!ELEMENT tutorial (name,url)&gt; &lt;!ELEMENT name (#PCDATA)&gt; &lt;!ELEMENT url (#PCDATA)&gt; &lt;!ATTLIST tutorials type CDATA #REQUIRED&gt;")
    猜你喜欢
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多