ryanprayogo ryanprayogo - 1 year ago 154
HTML Question

Pretty print XML data in JSP

How do I pretty print (ie. with indentation) XML data in the JSP? I have the following code:

<c:forEach items="${stuffs}" var="stuff">
<c:out value="${stuff}" escapeXml="true"/><br/>

But the problem is when
is an unformatted XML, it will show in the jsp as one long XML data. I need it pretty-printed inside the

Answer Source

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:apply-templates select="node()|@*" />