Meaningless Drivel is fun!*
The moose likes JSP and the fly likes Displaying XML as XML in HTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Displaying XML as XML in HTML" Watch "Displaying XML as XML in HTML" New topic
Author

Displaying XML as XML in HTML

Frank Silbermann
Ranch Hand

Joined: Jun 06, 2002
Posts: 1385
I am writing some web pages which display XML as XML (e.g., XML examples in a tutorial). I would prefer not to have to hand-code every angle-bracket. Also, since the XML is computer generated, I would prefer to have pretty-printing whitespace automatically generated.
Does anyone know of a java program available to do this, i.e. to automatically pretty-print XML source within an HTML web page?
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Originally posted by Frank Silbermann:
I am writing some web pages which display XML as XML (e.g., XML examples in a tutorial). I would prefer not to have to hand-code every angle-bracket. Also, since the XML is computer generated, I would prefer to have pretty-printing whitespace automatically generated.
Does anyone know of a java program available to do this, i.e. to automatically pretty-print XML source within an HTML web page?

Actually, JSTL's most fundamental tag -- <cut> -- will automatically escape XML metacharacters for you. This might not be enough (e.g., it's clearly not designed to do pretty-printing), but it's probably a good starting point.


Shawn Bayern<br />"JSTL in Action" <a href="http://www.jstlbook.com" target="_blank" rel="nofollow">http://www.jstlbook.com</a>
Pete Cassetta
Greenhorn

Joined: Feb 13, 2001
Posts: 23
Originally posted by Frank Silbermann:
I am writing some web pages which display XML as XML (e.g., XML examples in a tutorial). I would prefer not to have to hand-code every angle-bracket. Also, since the XML is computer generated, I would prefer to have pretty-printing whitespace automatically generated.
Does anyone know of a java program available to do this, i.e. to automatically pretty-print XML source within an HTML web page?

Frank,
I think you will find this article by Henri Yandell useful:
Generating XML via Java.
I have faced this issue of writing XML myself, and have found two workable solutions: JDOM and Henri Yandell's XmlWriter class. After reading Henri's article, I corresponded with him and added a few features to XmlWriter. I'm now using it in a heavy production environment with good results.
However, JDOM is also quite efficient, very elegant, and much more full-featured than XmlWriter. You should probably become familiar with it if you are not already. See the JDOM web site for details.
Frank Silbermann
Ranch Hand

Joined: Jun 06, 2002
Posts: 1385
Pete,
I like your suggestion for using JDOM to build up the XML document in memory rather than constructing it as a string directly. But my main question is, how do I pretty-print an XML fragment as, say, an element of an HTML table?
JDOM's XMLOutputter class can pretty-print the XML fragment as an ASCII text file. But how do I put it in my HTML page so that the browswer respects the tab and new-line whitespace, and doesn't try to interpret the XML text as HTML?
Shawn Bayern
Author
Ranch Hand

Joined: May 06, 2002
Posts: 160
Originally posted by Frank Silbermann:
Pete,
I like your suggestion for using JDOM to build up the XML document in memory rather than constructing it as a string directly. But my main question is, how do I pretty-print an XML fragment as, say, an element of an HTML table?
JDOM's XMLOutputter class can pretty-print the XML fragment as an ASCII text file. But how do I put it in my HTML page so that the browswer respects the tab and new-line whitespace, and doesn't try to interpret the XML text as HTML?

Once you have a string of XML that you want to output, simply pass it to JSTL's <cut> tag. That is, supposing that you have a session-scoped attribute named 'xml', you'd write

See http://java.sun.com/products/jstl for more information on JSTL.
[ August 20, 2002: Message edited by: Shawn Bayern ]
Pete Cassetta
Greenhorn

Joined: Feb 13, 2001
Posts: 23

Now your "xml" bean (that Shawn's code references) is initialized and ready to go.
[ August 20, 2002: Message edited by: Pete Cassetta ]
[ August 21, 2002: Message edited by: Pete Cassetta ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Displaying XML as XML in HTML
 
Similar Threads
Customising wsimport classes
Web Sevice vs Web Application
Source generator in castor
Restful java client
Soap Logging in Axis Tool