开发者

Can I escape HTML entities in XML so that they are displayed literally on a web page?

I have an XML descriptor file containing attribute specifications such as:

<attribute
  name="abc"
  description="xyz e.g. &lt;br&gt; with a new line">
...
</attribute>

These XML descriptors are parsed by a groovy script to produce HTML documentation (among other things) along the lines of:

<table>
  <开发者_开发问答;tr><th>Name</th><th>Description</th></tr>
  <tr><td>abc</td><td>xyz e.g. <br>with a new line</td></tr>
</table>

My question is what do I have to put in the XML to display HTML entities as character literals? e.g. A less than sign, such as:

<table>
  <tr><th>Name</th><th>Description</th></tr>
  <tr><td>abc</td><td>You must supply a &lt;port number&gt;</td></tr>
</table>

(The groovy script does no processing on the XML description - just prints it into the HTML file.)


Escape the ampersands in the HTML entities so you get the following:

<attribute
  name="abc"
  description="You must supply a &amp;lt;port number&amp;gt;">
...
</attribute>

The attribute value will then be seen by the Groovy script as:

You must supply a &lt;port number&gt;


You can simply replace '&' to '&amp;' to work around it. Here is the code:

<div id="d"></div>
<script type='text/javascript'>
var str = "<table><tr><th>Name</th><th>Description</th></tr><tr><td>abc</td><td>You must supply a  &amp;lt;port number &amp;gt;</td></tr></table>";
document.getElementById('d').innerHTML = str;
</script>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜