1

I use:

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

From this question Java: How to Indent XML Generated by Transformer , but as someone says: it doesn't ident the inner nodes as we expect to (it does ident them but not with 4 spaces).

So it's 4 spaces for first level of identation and 2 spaces for the next level like:

<a>
    <b>
      <b_sub></b_sub>
    </b>
    <c></c>
  </a>

Numbering the spaces:

<a>
 (4)<b>
   (2)<b_sub></b_sub>
 (4)</b>
 (4)<c></c>
(2)</a>

Can we ident nodes with 4 spaces (or with 1 tab if possible?)?

Source code:

DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();

NodeList rootlist = doc.getElementsByTagName("root_node"); //(example name)
Node root = rootlist.item(0);

root.appendChild(...);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("output.xml"));

transformer.transform(source, result);
Community
  • 1
  • 1
user2692669
  • 461
  • 8
  • 23
  • how many spaces is it using to do the indent? It doesn't help us answer when you don't supply information. – Bob Dalgleish Nov 06 '13 at 13:35
  • It uses 2 (I think it's like a default setting). (4 spaces for the first level of identation and 2 spaces for the second level.) – user2692669 Nov 06 '13 at 16:08
  • @BobDalgleish I edited it as you requested. – user2692669 Nov 06 '13 at 16:29
  • I suspect there are some whitespace-only text nodes in the output tree produced by the transformation, _before_ the serializer gets its hands on the data to add its own indentation. Indentation added by the serializer will only look sensible if there's no additional whitespace there to start with. What does your input XML and XSLT look like - are you doing `xsl:strip-space` on the input? – Ian Roberts Nov 06 '13 at 16:59
  • @IanRoberts I put the source code in the end. – user2692669 Nov 06 '13 at 17:14

1 Answers1

2

The indentation added by the outputter will not look right if the original input XML was itself indented. Rather than a no-op identity transformer you could try using a simple XSLT to strip out any indentation in the original XML before re-indenting it:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource(new StringReader(
  "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'\n"
+ "    xmlns:xalan='http://xml.apache.org/xalan'>\n"
+ "  <xsl:strip-space elements='*' />\n"
+ "  <xsl:output method='xml' indent='yes' xalan:indent-amount='4' />\n"
+ "  <xsl:template match='/'><xsl:copy-of select='node()' /></xsl:template>\n"
+ "</xsl:stylesheet>"
));
Transformer transformer = transformerFactory.newTransformer(xsltSource);

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("output.xml"));

transformer.transform(source, result);
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Worked like a charm. Is there any guide/site with more info about your answer? Thank you! – user2692669 Nov 06 '13 at 18:31
  • 1
    @user2692669 the important bit is the `xsl:strip-space`, which is described in the [XSLT specification](http://www.w3.org/TR/xslt#strip). – Ian Roberts Nov 06 '13 at 18:37