This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Displaying XML as XML in HTML

 
Frank Silbermann
Ranch Hand
Posts: 1407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pete Cassetta
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic