如何在同一页面中显示XML和其他类型的数据?
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<country>Columbia</country>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
Run Code Online (Sandbox Code Playgroud)
上面的XML应该按原样显示格式.另外,我想在我的页面中显示HTML表格和其他内容.怎么做到这一点?
mel*_*okb 85
简单的解决方案是嵌入<textarea>元素内部,这将保留格式和尖括号.我还删除了style="border:none;"使textarea不可见的边框.
这是一个示例:http://jsfiddle.net/y9fqf/1/
Keo*_*bre 34
您可以使用旧<xmp>标签.我不知道浏览器支持,但它仍然可以工作.
<HTML>
your code/tables
<xmp>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<country>Columbia</country>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
</xmp>
Run Code Online (Sandbox Code Playgroud)
输出:
your code/tables
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<country>Columbia</country>
<price>10.90</price>
<year>1985</year>
</cd>
</catalog>
Run Code Online (Sandbox Code Playgroud)
小智 16
<pre lang="xml" >{{xmlString}}</pre>
Run Code Online (Sandbox Code Playgroud)
这对我有用.感谢http://www.codeproject.com/Answers/998872/Display-XML-in-HTML-Div#answer1
Jac*_*cob 15
如果将内容视为文本而不是HTML,则DOM操作应该使数据正确编码.这是你在jQuery中的表现:
$('#container').text(xmlString);
Run Code Online (Sandbox Code Playgroud)
以下是使用标准DOM方法的方法:
document.getElementById('container')
.appendChild(document.createTextNode(xmlString));
Run Code Online (Sandbox Code Playgroud)
如果您通过服务器端脚本将XML放在HTML中,那么必然会有编码功能允许您这样做(如果您添加了服务器端技术,我们可以为您提供具体的示例'做到了).