如何使用Java将内部dtd插入到xml文件中

时间:2011-07-20 07:14:13

标签: java xml dtd

如何使用Java

将内部dtd插入xml文件

我有以下测试代码,它将XML文件读入DOM文档,然后再将其写出来。我希望输出文件最终与输入文件相同。

public class DomToXml {
Document document;

void process() {
    document = parseXmlFile("dat/input.xml");
    writeXmlFile(document, "dat/output.xml");
}

void writeXmlFile(Document document, String fileName) {
    try {
        Source source = new DOMSource(document);
        File file = new File(fileName);
        Result result = new StreamResult(file);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        DocumentType documentType = document.getDoctype();
        if (documentType != null) {
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, documentType.getSystemId());
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, documentType.getPublicId());
        }
        transformer.transform(source, result);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
}

Document parseXmlFile(String filename) {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setXIncludeAware(true);
    documentBuilderFactory.setExpandEntityReferences(true);
    documentBuilderFactory.setValidating(false);
    DocumentBuilder documentBuilder;
    try {
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
        documentBuilder.setErrorHandler(new MyErrorHandler());
        documentBuilder.setEntityResolver(new MyEntityResolver());
        return documentBuilder.parse("file:" + filename);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
    }
    return null;
}

public static void main(String[] args) {
    DomToXml a = new DomToXml();
    a.process();
    System.out.println("Done!");
}

}

输入文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE root PUBLIC "NONE"
  "none.dtd" [
  <!ENTITY foo "bar">
]>
<root id="TEMPLATE" xreflabel="TEMPLATE-&foo;">
</root>

输出:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE root PUBLIC "NONE" "none.dtd">
<root id="TEMPLATE" xreflabel="TEMPLATE-bar">
</root>

正如您所看到的,dtd以及实体信息并未被输出。 我该如何解决这个问题?

作为次要问题,我如何停止实体的转型。即我希望输出文件包含TEMPLATE-&amp; foo;而不是TEMPLATE-bar。

谢谢,

韦恩。

2 个答案:

答案 0 :(得分:0)

我可以在输出中获取DTD实体声明,但还没有弄清楚如何保留实体本身:

DOMImplementationLS domImplementation = (DOMImplementationLS) DOMImplementationRegistry
    .newInstance().getDOMImplementation("LS");
LSSerializer lsSerializer = domImplementation.createLSSerializer();
System.out.println(lsSerializer.writeToString(document));

答案 1 :(得分:0)

将String转换为DOM的简单方法(反之亦然)可能会出现在这里:

http://famulatus.com/ks/programming/java/item/335-convert-xml-dom-to-string-and-string-to-xml-dom.html