File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes How do I pretty print xml? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How do I pretty print xml?" Watch "How do I pretty print xml?" New topic
Author

How do I pretty print xml?

William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Is there a simple way of pretty printing xml? Thanks.


Please ignore post, I have no idea what I am talking about.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Pretty print in what way? You can use XSL, of course, to make XML look however you want.
Moving to XML forum.
[ March 28, 2004: Message edited by: Thomas Paul ]

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Pretty-printing into what media (to a web page or an XML file)?
One way to achieve the former could be to use the XMLSerializer class from Xerces-J and hand it a custom Writer/OutputStream which encodes all special characters such as "<" and ">" into their HTML entity counterparts. Just an idea -- I really don't know how well this would work.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I want the xml, not the data in the xml, to be presented to standard out with line breaks, tabs, ... anything needed to make it read-able.
Doesn't XSL only work on how the data in the xml is handled?
I will checkout the XMLSerializer and see if that gives me what I want.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by William Barnes:
Doesn't XSL only work on how the data in the xml is handled?
No. You can copy elements from the source XML into the target XML with XSL's <copy-of/> element, including attributes, namespaces, etc.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

No. You can copy elements from the source XML into the target XML with XSL's <copy-of/> element, including attributes, namespaces, etc.

Ok. But does that help me solve my problem in anyway? Thanks.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by William Barnes:
Ok. But does that help me solve my problem in anyway? Thanks.
It does, because while your XSL templates only copy what's in the source XML file, it produces an indented version of it if you plug a <xslutput method="xml" indent="yes"/> on top of your stylesheet.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I guess that ends the discussion. Now off to learn how to xsl. Thanks.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Ok, so I am running xalan from the command line (the one which comes with Websphere 4.0). My input xml is:

My template/stylesheet is:

And I get the values from the xml, not the xml, output to the screen. The output is actually the same as not having the line And if I change the output method to "html" I still get only the values from the input xml.

My output is always:

Any ideas about what I am doing wrong here? And yes I had to remove the ":" between "xml" and "output" to get rid of those damn smilies. Thanks.
[ April 02, 2004: Message edited by: William Barnes ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
You must specify at least one <xsl:template> which uses the <xsl:copy-of> element to copy the source tree to the result tree as is, but with the indentation.
Googling for "pretty print xml xsl" gives some examples.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Usually I google things, don't know why I didn't do it this time. That example was helpful, but there were not many others.
Ok, so I have this working - it breaks up the xml - but the indentation still isn't working. Can this be a limitation with xalan? Do I need to try another XSL processor?
Input xml:

xslt:

Output xml:

Thanks.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I think you should try other XSL engines. If you're driving the XSL transformation from Java code, you might also want to try Transformer#setOutputProperty(OutputKeys.INDENT, "yes")
Also, if you can use Xerces-specific functionality, there's a handy class called XMLSerializer which produces nicely indented XML.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

In case anyone is interested (hopefully this isn't stating the obvious).
How to run xalan (an xslt engine) from the command line:
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Ok, finally figured how to do this with XSL. And wow is it s l o w !

[ April 21, 2004: Message edited by: William Barnes ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Bill, I took the liberty of adding your XSL into our XmlFaq. I hope it's ok?
 
GeeCON Prague 2014
 
subject: How do I pretty print xml?