Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

XML Pretty Printing...

Posted on 2006-10-27
2
Medium Priority
?
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 240 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 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.
Suggested Courses

598 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