使用Axiom解析器漂亮地打印OMElement

时间:2018-07-04 13:32:39

标签: axiom

在Axiom中,我无法为OMElement提供缩进选项。使用序列化方法omelement被写入文件中。但这不是漂亮的印刷品。

Axiom解析器中是否有可用的选项?我以前写到文件中的代码。

 FileOutputStream fileOutputStream= new FileOutputStream("filename");
 XMLOutputFactory xmlOutputFactory= XMLOutputFactory.newInstance();
 XMLStreamWriterwriter = xmlOutputFactory.createXMLStreamWriter(fileOutputStream);
 omElement.serializeAndConsume(writer);
 writer.flush();
 writer.close();

1 个答案:

答案 0 :(得分:0)

您可以使用Transformer添加缩进,如以下代码片段所示:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(omElement.getSAXSource(true), new StreamResult(fileOutputStream));
相关问题