xml分离后的java没有换行符

时间:2018-09-06 18:39:21

标签: xml parsing dom w3c transformer

在修改xml文档之后,我已经将该文档写入了一个新文件。但是,在此xml文件中,xml声明后缺少换行符:

<?xml version="1.0" encoding="utf-8" standalone="no"><!DOCTYPE EMail SYSTEM "../src/email.dtd">
<Mail><Person>John</Person>
<Number>7</Number>
</Mail>

public void createDoc(File outputFile) throws FileNotFoundException, IOException, TransformerConfigurationException,
    TransformerException, TransformerFactoryConfigurationError {

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, this.dtdName);
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(new DOMSource(this.doc), new StreamResult(new OutputStreamWriter(new FileOutputStream(outputFile))));
}

为什么xml decl和doctype标记之间没有换行符?如果我使用记事本打开文件并想立即换行,则记事本会崩溃。那是Java的错误吗?

0 个答案:

没有答案