Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Java to use tabs instead of spaces for indenting

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?

like image 493
tumba25 Avatar asked Apr 02 '10 01:04

tumba25


2 Answers

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, then you can use an afterburner XSLT-script on the output which does whatever you want to do on text()-nodes.

like image 143
Thorbjørn Ravn Andersen Avatar answered Sep 17 '22 17:09

Thorbjørn Ravn Andersen


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.

like image 33
ring bearer Avatar answered Sep 18 '22 17:09

ring bearer