Print a XML string to browser doesn’t work in JSP


XML & Web services: Print a XML string to browser doesn’t work in JSP

  1. <%
    String xmlReq = generateXML();

    xmlReq is the XML string, and I want to print it out in browser, but it doesn't
    print it out. But when I go to view|source, then I could see the XML.

    any ideas? please help. thanks!!
  2. The browser is trying to interpret the xml tags as HTML tags and, failing to do so, is ignoring them.

    The simplest solution is to wrap the XML output in HTML <PRE> tags or in a CSS-styled block with the 'preformatted' attribute set (I forget offhand what the actual name of that attribute is).
  3. Even wrapping the XML string in tags still won't work, a web browser will assume the tags are HTML and render or ignore them. You will need to replace each < and > character to < and > (see the very useful String.replaceAll() method)
  4. Sorry, my reply was automatically escaped. I hope this one works better!

    tags won't work, the browser will still try to render tags within the "pre" block and ignore any that aren't HTML.

    You need to replace < with &amp;lt; and > with &amp;gt;