无法使用java将doctype标记添加到xml文件

时间:2016-06-09 13:58:24

标签: java xml sax dtd

我寻求你的帮助,我有一个自动生成的XML文件,我需要根据DTD验证,所以我必须修改生成的XML来添加doctype标签,当我使用下面的代码时,我没有添加咨询了许多线索,但没有积极的结果。

以下是我的代码中的相关部分:

InputStream inputStream= new FileInputStream(extractedFileURL);
Reader reader = new InputStreamReader(inputStream,"UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");

Document doc = docBuilder.parse(is);
///Add Doctype declaration to the extracted XML File
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");
org.w3c.dom.DOMImplementation domImpl = doc.getImplementation();
DocumentType doctype = domImpl.createDocumentType("doctype",
    "SYSTEM"
        ,
    "registry\\ixb\\dtds\\extractor.dtd\\generatedDTD1.dtd");
//transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(extractedFileURL));
transformer.transform(source, result);

0 个答案:

没有答案