Solved

XML Pretty Printing...

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
javap bin 2 41
null output 3 35
How to determine if a string is a valid SHA value 7 35
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

861 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