TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
DOMSource source = new DOMSource(document);
StreamResult stream = new StreamResult(os);
transformer.transform(source, stream);
However, found it doesn't work on jdk5.
I need to change to the following work around code
TransformerFactory transFactory = TransformerFactory.newInstance();
transFactory.setAttribute("indent-number",
new Integer(2));
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(document),
new StreamResult(new OutputStreamWriter(os, "utf-8")));
10 comments:
Hi there!
many thanks for this post. I had no problem to solve about the output stream configuration (utf-8), but for me the "indent" didn't work at all. Here I learned about the
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
and now the world is wunderbar again. :-)
Hey!
Your post helped me a lot^^
Thanks!
See also http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6337981
it is very funny how you express it worked with 1.4 does not work with 1.5
Even the new posted solution does not work with "my 1.5 runtime"
ane even old code "did not work" with my 1.4 runtime.
You should be more specific about exact version of Factorie and transformer implementation classes and tehir version folks ;-)
Hi,
Using the OutputStreamWriter works!..Thank you
Yes, finally OutputStreamWriter worked! (use Java 1.5)
This worked for me. Thanks a lot!
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
thanks a lot
Where is the os variable declared?
In JDK 6, I just needed to add this line to the transformer: transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Post a Comment