我的Java程序是这样的:的Java XML自闭标签
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风格只允许我想要的输出。如何实现这一目标?是否有任何输出属性的变压器或任何属性DocumentBuilderFactory要做输出?
[This question](http://stackoverflow.com/questions/5910963/how-to-make-javax-transformer-output-html-no-self-closing-tags)是类似的,并且有一些很好的讨论。 –
[This question](http://stackoverflow.com/questions/1560762/xslt-wont-allow-me-to-use-self-closing-img-and-br-tags/1560815#1560815%5D)可能有帮助也是。 –