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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With