我的 Java 程序如下所示:
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
try {
db = dbf.newDocumentBuilder();
Document document = db.parse(new ByteArrayInputStream("<test><test1></test1></test>".getBytes("UTF-8")));
StringWriter stringWriter = new StringWriter();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
System.out.println(stringWriter.toString());
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
输出是:<test><test1/></test>
我想要输出<test><test1></test1></test>
。
因为我使用JasperReports和html样式只允许我想要的输出。如何做到这一点?是否有 Transformer 的任何输出属性或DocumentBuilderFactory的任何属性来做想要的输出?