如何使用 DOM (JAVA) 在 XML 文档中添加 Doctype

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13553614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:12:25  来源:igfitidea点击:

How to add Doctype in XML document using DOM (JAVA)

javaxmldomdoctype

提问by wali

I have created a XML document using DOM in java. I am unable to add doctype. I want the doctype like this.

我在 Java 中使用 DOM 创建了一个 XML 文档。我无法添加文档类型。我想要这样的文档类型。

<!DOCTYPE IndInfo PUBLIC "EDAFileSomething" "EDAFileSomething_2_0.dtd">

Here is the document creation code.

这是文档创建代码。

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();

Here is the Transformer Object Code.

这是变压器目标代码。

TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = null;
            try {
                transformer = transformerFactory.newTransformer();
            } catch (TransformerConfigurationException ex) {
                Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
            }
            DOMSource source = new DOMSource(doc);
            try {

                StreamResult result = new StreamResult(System.out);
                transformer.transform(source, result);
            } catch (TransformerException ex) {
                Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
            }

            System.out.println("File saved!");

回答by Adam

You can construct the doctype with the DOM and set the doctype as an output property.

您可以使用 DOM 构造文档类型并将文档类型设置为输出属性。

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
DOMImplementation domImpl = document.getImplementation();
DocumentType doctype = domImpl.createDocumentType("doctype",
    "-//Oberon//YOUR PUBLIC DOCTYPE//EN",
    "YOURDTD.dtd");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(database));
transformer.transform(source, result);

回答by Magnus

If you get an instance of DOMImplementationby invoking the getDOMImplementation()method on the DocumentBuilder, you can use the createDocumentmethod to create a new Documentwith the specified doctype.

如果DOMImplementation通过调用 上的getDOMImplementation()方法获得 的实例 DocumentBuilder,则可以使用该createDocument方法创建Document具有指定文档类型的新对象。

It also has a createDocumentTypemethod for creating a DocumentTypeobject

它还有一个createDocumentType创建DocumentType对象的方法

Refer to http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/DOMImplementation.htmland http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.htmlfor more info.

请参阅http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/DOMImplementation.htmlhttp://docs.oracle.com/javase/1.5.0/docs/ api/javax/xml/parsers/DocumentBuilder.html了解更多信息。