views:

284

answers:

3

I'm creating an XML document. I got it to indent using TransformerFactory.setAttribute("indent-number", new Integer(2)); Transformer.setOutputProperty(OutputKeys.INDENT, "yes");

Is it possible to get Java to use tabs instead of spaces for indenting? And how?

A: 

I think not.

You can get the result and process it, replacing calling str.replace("\n ", "\n\t") (or whatever number of spaces you want).

But I'd recommend against that - tabs aren't platform independent.

Bozho
@Bozho, it may be dangerous to do a `str.replace` call as it might end up replacing some attribute values etc to tab from space; very undesirable isn't it?
ring bearer
you are right - I added an update by adding a new line.
Bozho
+1  A: 

Yes, tabs are considered to be evil by a few. However, if you want to use TransformFactory and want to change the indenting behavior to use tabs instead of spaces, you need to provide your own implementation of ContentHandler. Then pass your implementation of ContentHandler into a new SAXResult - pass that as the "result" to the Transformer.transform(...). Lot of hoops to jump through. Another consideration may be to use a smart XSLT over your output.

ring bearer
+1  A: 

No, not in general. The XSLT specification does not allow for specifying WHAT whitespace to use when indenting.

It might, however, be a XSLT-processor specific item to configure. Check the documentation for the one you are using.

If you REALLY want this, and you can use an afterburner XSLT-script on the output which does whatever you want to do on text()-nodes.

Thorbjørn Ravn Andersen