0

So I have a servlet which prints content of various files. But when I want to print .xml file my servlet page doesn't print anything, because page uses this xml tags as html and is parsing them istead of printing. And I want to print this tags. I am reading file line by line and lines are stored in variable line.

2 Answers2

2

If you are attempting to Display XML as content in an HTML document:

Browsers can't tell the difference better a < that the author intends to mean "Start of tag" and one that the author intends to mean "Render this".

You need to represent it as &lt; if you want it to appear as data.

The answer to htmlentities equivalent in JSP? explains how to convert a string of text into a string of HTML.


If you are attempting to Output an XML document instead of an HTML document:

You need to specify an XML content type (such as application/xml) instead of an HTML content-type.

See How to set the content type on the servlet for an explanation.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

If you want to print xml content in your HTMl page, you can use StringEscapeUtils.escapeHtml() function from Apache commons lang library to write xml file contents to your HTML page

PrintWriter writer = response.getWriter();
writer.write("<html><head></head><body>");
writer.write(StringEscapeUtils.escapeHtml(xmlContent);
writer.write("</body></html>");
maneesh
  • 1,092
  • 1
  • 8
  • 11