Solved

XML Pretty Printing...

Posted on 2006-10-27
2
236 Views
Last Modified: 2010-03-31
I am trying to set the indenting of an xml doc so it is easily readable but it is not working?

I would like to stick with the javax.xml.transform classes...

Here is the code I am using


      private void indentXML(Document doc){

            try{
                  Transformer trans = TransformerFactory.newInstance().newTransformer();
                  trans.setOutputProperty(OutputKeys.INDENT, "yes");
                  trans.transform(new DOMSource(doc), new StreamResult(System.out));

            }catch(Exception e){}
      }
0
Comment
Question by:defa0009
2 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 60 total points
ID: 17819435
Workaround could be found here
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5064280

(1)set the indent-number into the transformerfactory
TransformerFactory tf = new TransformerFactory.newInstance();
tf.setAttribute("indent-number", new Integer(2));
(2)enable the indent in the transformer
Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
(3)wrap the otuputstream with a writer (or bufferedwriter)
t.transform(new DOMSource(doc),
new StreamResult(new OutputStreamWriter(out, "utf-8"));
0
 
LVL 1

Author Comment

by:defa0009
ID: 17819544
Your the man!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
Java Server Faces parameter pass? 6 50
Configure a Bean in an XML file 4 32
difference between sorce folder and folder in eclipise 3 13
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question