0

i want to show a xml in a jsp page as it is. Currently when i am displaying it on a jsp page then it is removing all xml structure like nodes and just showing the content. For eg. for

    <file>
        <name>12345.pdf</name>
        <size>70725bytes</size>
        <isDirectory>No</isDirectory>
    </file>

It is showing on ui as only this:-

        <name>12345.pdf</name>
        <size>70725bytes</size>
        <isDirectory>No</isDirectory>
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
TheUser
  • 129
  • 1
  • 1
  • 10
  • Where is the code of your JSP? – JB Nizet Dec 21 '13 at 17:27
  • in servlet below is the code:- request.setAttribute("outputDir", traceDirResp); getServletContext().getRequestDispatcher("/TraceDirectory.jsp").forward(request, response); in jsp page below is the code:-
    – TheUser Dec 21 '13 at 17:56

2 Answers2

0

to be shown as text and not be interpreted as markup, < must be escaped. Escaping > is not absolutely required, but I would do it anyway:

&lt;file&gt;
    &lt;name&gt;12345.pdf&lt;/name&gt;
    etc.

If the XML is in fact store in a request attribute, you just need to escape it thanks to the JSTL c:out tag (just like every value that could contain special characters):

<c:out value="${theXmlString}"/>
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Event after using , i am getting the same problem. can you suggest if i am going wrong somewhere because when i am taking output on console it is coming fine but when i pass it from servlet to jsp page for showing on jsp page it is not coming in exact xml format. – TheUser Dec 21 '13 at 17:38
  • I can't tell if you're doing something wrong without seeing any single line of your code. Edit your question, and show your code. – JB Nizet Dec 21 '13 at 17:45
  • in servlet below is the code:- request.setAttribute("outputDir", traceDirResp); getServletContext().getRequestDispatcher("/TraceDirectory.jsp").forward(request, response); in jsp page below is the code:-
    – TheUser Dec 21 '13 at 18:01
0

put your tag between pre tags

out.print("<pre>"+yourXML+"</pre>");

it worked for me

Muhammad
  • 6,725
  • 5
  • 47
  • 54
  • Even inside a pre tag, the special characters must be escaped. It works only beause the browser tries to fix the errors for you. – JB Nizet Dec 21 '13 at 17:46
  • Ya i had put the xml in cout between pre tags but this didn't workout for me. without Pre Tags it is showing whole xml in a single line. – TheUser Dec 21 '13 at 17:50
  • @manish: that is expected. sequences of white spaces (including line breaks) are considered as a single space in HTML, unless they're inside a pre tag. So c:out properly escapes the brackets, and `
    ` makes the browser display every character as a text editor would do.
    – JB Nizet Dec 21 '13 at 17:53