How do I pretty print (ie. with indentation) XML data in the JSP? I have the following code:
<c:forEach items="${stuffs}" var="stuff">
<pre>
<c:out value="${stuff}" escapeXml="true"/><br/>
</pre>
</c:forEach>
But the problem is when ${stuff}
is an unf开发者_如何学运维ormatted XML, it will show in the jsp as one long XML data. I need it pretty-printed inside the <p>
tag.
XSLT has a simple means of doing this via the xsl:output
element. If you can apply an XSLT, I suggest using a stylesheet like this (based on the identity transformation):
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*" />
<xsl:output method="xml" indent="yes" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If you want a simple solution, don't bother with xsl while setting response for JSP to look at, just do stuff.replaceAll("<", "& lt;").replaceAll(">","& gt; ");
You don't need anything else; no XSL transformation needed here. Use technologies when they are essential, unless I am missing something here.
You can pretty print JSLT with Pretty Diff at http://prettydiff.com/?m=beautify It will do exactly what you need. Consider the following example:
<a>
<c:out value="<strong>some content</strong>"/>
</a>
Pretty Diff is capable of recognizing multidimensional tags so long as the nested tag is in quotes.
精彩评论