aspose file tools*
The moose likes Servlets and the fly likes How to generate HTML to display XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to generate HTML to display XML" Watch "How to generate HTML to display XML" New topic
Author

How to generate HTML to display XML

Frank Silbermann
Ranch Hand

Joined: Jun 06, 2002
Posts: 1390
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

Joined: Feb 20, 2001
Posts: 580
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, President<br />Carman Consulting, Inc.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Sorry, that was supposed to be the <out> custom tag with the "c" namespace.
Frank Silbermann
Ranch Hand

Joined: Jun 06, 2002
Posts: 1390
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

Joined: Jun 06, 2002
Posts: 1390
Although, I suppose I could access code in a tag library directly from Java, outside the context of a JSP, no?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to generate HTML to display XML