我有一个只包含一个元素的 XML 文件
<Message>
<Location URI ="XXX:XXX:XXX" />
</Message>
我想使用 Java 读取和打印相同的 XML,但打印后它在 /> 之前丢失了空格
<Message>
<Location URI ="XXX:XXX:XXX"/>
</Message>
我尝试了 DocumentBuilderFactory 和 Transformer 的不同配置,但结果是一样的。
任何想法?
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document requestDocument = builder.parse(this.getClass().getResourceAsStream("/message-template.xml"));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
DOMSource domSource = new DOMSource(requestDocument);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
transformer.transform(domSource, result);
System.out.println(writer.toString());
相关分类