Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to generate HTML to display XML

 
Frank Silbermann
Ranch Hand
Posts: 1406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet that generates a three-column multi-row table. In the elements of one column I would like to display pretty-printed fragments of XML code.
The trouble is that the browser tries to interpret XML tags and HTML, fails to recognize the tags and ignores them.
One solution is to escape all the special characters (e.g. '<') to avoid this. In addition, I would want the browser to respect the whitespace (blanks and newlines) which happen to appear in the XML document. The XML is programmatically generated, so the conversion to HTML would have to be implemented as a method called by my servlet.
I must assume that this is a solved problem, and I don't want to re-invent the wheel. Where can I find a tool, and API method, or some code to help me with this?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Silbermann:
I have a servlet that generates a three-column multi-row table. In the elements of one column I would like to display pretty-printed fragments of XML code.
The trouble is that the browser tries to interpret XML tags and HTML, fails to recognize the tags and ignores them.
One solution is to escape all the special characters (e.g. '<') to avoid this. In addition, I would want the browser to respect the whitespace (blanks and newlines) which happen to appear in the XML document. The XML is programmatically generated, so the conversion to HTML would have to be implemented as a method called by my servlet.
I must assume that this is a solved problem, and I don't want to re-invent the wheel. Where can I find a tool, and API method, or some code to help me with this?

You might want to move your presentation into a JSP page and try using some custom tag library (Such as Struts or JSTL) which has tags that auto-escape your output. Take a look at the Struts <bean:write> or the JSTL <c ut> tags. To keep your spacing consistent with the source, you could place the text inside a <pre> element.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, that was supposed to be the <out> custom tag with the "c" namespace.
 
Frank Silbermann
Ranch Hand
Posts: 1406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's one approach. In this case, my HTML returned by the servlet is generated from XML via an XSL stylesheet. I really don't to write a stylesheet to generate a JSP -- that would involve all sorts of administration issues. So I'm really looking for a method that's independent of the JSP container.
 
Frank Silbermann
Ranch Hand
Posts: 1406
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although, I suppose I could access code in a tag library directly from Java, outside the context of a JSP, no?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic