3
<?xml version="1.0" encoding="UTF-8"?><Request>

<Id>

这里第一个标签被写入文件,然后是 xml 版本,我需要在下一行写入。像下面

<?xml version="1.0" encoding="UTF-8"?>
 <Request>
  <Id>

我已经使用下面的代码将 xml 写入文件,请帮助

TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File(
                Constants.xmlLocation
                        + Constants.metaDataXmlFileName
                        + format.format(calendar.getTime()) + ".xml"));

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(
                "{http://xml.apache.org/xslt}indent-amount", "5");
        transformer.transform(source, result);
4

1 回答 1

3

您可以尝试下面的代码,这将正常工作,您将获得格式化的输出

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "10");
于 2013-09-04T08:52:33.593 回答