0
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
tf.transform(new StreamSource(reader), new StreamResult(writer));

the above code gives me the following result:

<Response>
    <Head>ERROR</Head>
    <Body>
        <ERROR code="1000" reason="ServerSOAPFaultException" description="Fault occurred while processing."/>
    </Body>
</Response>

it does not indent xml-attributes, but I need xml-attributes to be indented as well:

<Response>
    <Head>ERROR</Head>
    <Body>
        <ERROR code="1000"
               reason="ServerSOAPFaultException"
               description="Fault occurred while processing."/>
    </Body>
</Response>

How to do it?

阿尔曼
  • 1,275
  • 2
  • 14
  • 18

1 Answers1

2

Use the Saxon serializer rather than the Xalan serializer, and if you want to force attributes to be stacked vertically even when they would fit horizontally, set a small value for the saxon:line-length property.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Saxon supports the JAXP interface so you can essentially use the code you've already got - just put Saxon on the class path. – Michael Kay Feb 25 '17 at 13:26
  • I have added [Saxon-HE dependency](https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE/9.7.0-15) to my project and it started indenting attributes, but it only works for the elements containing many attributes, and does not work for elements containing only 3-4 attributes. Could you explain how to set a small value for the saxon:line-length property ? – 阿尔曼 Feb 27 '17 at 09:51
  • LicenseException: Requested feature (custom serialization {http://saxon.sf.net/}line-length) requires Saxon-PE :( – 阿尔曼 Feb 28 '17 at 04:10
  • Yes, sorry about that, all extensions in the Saxon namespace require Saxon-PE or higher. – Michael Kay Feb 28 '17 at 12:02