• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I pretty print xml?

 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a simple way of pretty printing xml? Thanks.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 <xsl:output method="xml" indent="yes"/> on top of your stylesheet.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess that ends the discussion. Now off to learn how to xsl. Thanks.
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill, I took the liberty of adding your XSL into our XmlFaq. I hope it's ok?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic