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. <br> 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 <port number></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 &lt;port number&gt;">
...
</attribute>
The attribute value will then be seen by the Groovy script as:
You must supply a <port number>
You can simply replace '&' to '&
' 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 &lt;port number &gt;</td></tr></table>";
document.getElementById('d').innerHTML = str;
</script>
精彩评论