我想在标头中生成 1.1 作为版本的 xml 文件,因为在使用 1.0 版本解析我的 xml 文件时我得到 SaxparserExceptions:
"Character reference "" is an invalid XML character".
当我手动更改标题时,我没有收到任何错误。通过 outputkeys 更改版本似乎不起作用。该文件仍然有错误的标题:
"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"
示例代码:
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory
.newTransformer();
transformer.setOutputProperty(
OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.VERSION, "1.1");
transformer
.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount",
"2");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(
"processed_.xml");
transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}