Hi

I am not much experienced in JSP.

I am facing one problem while printing a XML String on the screen.

Code snippet ->

String result = ab.returnResult();

result is returning a XML String..Suppose <A>123</A><B>SIR</B><C>21</C>

I am printing -> <tr><td><%= result %></td></tr>

I am getting 123SIR21 as output in the screen..

I want it as -> <A>123</A><B>SIR</B><C>21</C> ( with XML tags )..


Could anyone help me out of this issue...
I am sorry If I am asking a silly question..Please answer as early as possible..

Dani AI

Generated

Quick recap: was printing a raw XML string inside an HTML table cell using a JSP expression, so the browser treated the angle brackets as HTML and hid the tags. 's idea to escape the markup is correct — you need to render the angle brackets as literal characters rather than as HTML elements.

A few practical, more robust options for JSP:

  • Use JSTL to escape automatically:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<td><c:out value="${result}" escapeXml="true" /></td>
  • Or escape on the server with a library helper (for example, Apache Commons Text's escapeHtml4) and print the escaped string. These approaches are safer and cleaner than hand-rolled regexes because they handle &, quotes and other edge cases.

If the goal is to get the browser's native XML viewer (collapsible nodes) as asked, the page must be served as pure XML — not embedded in an HTML page. That means sending only the XML and setting the response content type, for example:

response.setContentType("text/xml;charset=UTF-8");
out.print(result);

The XML must be well-formed (and optionally include an XML declaration). Browser behavior varies, so some browsers show a tree, others may not.

If you want an interactive XML tree inside an existing HTML page, parse the XML on the client (DOMParser) and render it with a small tree widget (jsTree, a jQuery XML viewer or a custom UL/LI walker). Troubleshooting tips: avoid double-escaping (don't pre-escape then use an escaping tag), ensure correct charset, and remember escaping when showing tags also protects against XSS. 's quick fix was the right direction; the options above are cleaner and more maintainable.

Recommended Answers

All 5 Replies

You need to escape the XML strings returned and convert the <, > and other characters which have special meaning in HTML to their .

< - &lt;
> - &gt;
" - &quot;

You can try something like:

String str = ab.result();
str = str.replaceAll("<", "&lt;").replaceAll(">", "&gt;");

thanks ~S.O.S~ for ur help...It worked..

Happy to help. :-)

any way to make the browser to present the string with the xml tags as real XML (i mean with the ability to click on the "+" and the "-", cause all it shows is a static string... and not xml...

?

No, there isn't any direct method of doing the same. This is because the representation you are talking about is the way browser is configured to / coded to display XML; this would differ from browser to browser.

If you want to make this happen, you would have to create a XML document instance from the XML received as string and apply some Javascript magic to convert that in memory tree structure to something of value for the client using a bunch of <ul> and <li> and what not. This is by no means a simple task unless you are well versed in Javascript.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.