Solved

XML Pretty Printing...

Posted on 2006-10-27
2
226 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 39
Java Server Faces parameter pass? 6 37
session migration servlets 2 25
replace a word with other 1 40
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now