Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

How to generate HTML to display XML  RSS feed

 
Ranch Hand
Posts: 1408
  • 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?
 
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: 1408
  • 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: 1408
  • 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
Boost this thread!