开发者

How can I insert a XML file in a XHTML page <textarea> using JSP?

How can I include a XML file as content in a textarea element in a XHTML document? It will cause validation errors if the special characters are not escaped.

Is there an easy way in JSP to escape special characters before they are inserted using the include directive, like using the JSTL?

Example code:

    <div>
        &l开发者_StackOverflowt;textarea name="content" rows="20" cols="80"><%@ include file="example.xml" %></textarea>
    </div>

This will look fine in a browser, but XHTML validation will fail because the embedded file starts another XML declaration.


The "official" JSTL way of doing this is as follows:

<c:import url="example.xml" var="xmlContent"/>

<textarea><c:out value="${xmlContent}" escapeXml="true"/></textarea>

The escapeXml attribute defaults to true anyway, but it's probably wise to specify it here, for reasons of documentation.


You probably want to embed your <%@ include %> within a <![CDATA[...]]> so it is considered as plain text rather than XML parts. You can read this to get a bit more insight on what CDATA is for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜