jQuery 在 HTML 页面中显示 XML 内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7519618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display XML content in HTML page
提问by karthi
How to display XML and other type of data in same page ?
如何在同一页面中显示 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>
The above XML should be displayed as it is with formatting. Also , I would like to display HTML tables and other stuff in my page. How to achieve this ?
上面的 XML 应该按格式显示。另外,我想在我的页面中显示 HTML 表格和其他内容。如何实现这一目标?
- I do get XML in STRING not through file.
- I do not want to parse it
- I want to display (that's it)
- If you say about XSLT ( please give example )
- I'm thinking to use Jquery plugin ( any examples?)
- 我确实在 STRING 中获取 XML,而不是通过文件。
- 我不想解析它
- 我想显示(就是这样)
- 如果你说 XSLT(请举例)
- 我正在考虑使用 Jquery 插件(有什么例子吗?)
回答by mellamokb
Simple solution is to embed inside of a <textarea>
element, which will preserve both the formatting and the angle brackets. I have also removed the border with style="border:none;"
which makes the textarea invisible.
简单的解决方案是嵌入<textarea>
元素内部,这将保留格式和尖括号。我还删除了style="border:none;"
使 textarea 不可见的边框。
Here is a sample: http://jsfiddle.net/y9fqf/1/
这是一个示例:http: //jsfiddle.net/y9fqf/1/
回答by Keombre
You can use the old <xmp>
tag. I don't know about browser support, but it should still work.
您可以使用旧<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>
Output:
输出:
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>
回答by mcsekar
<pre lang="xml" >{{xmlString}}</pre>
This worked for me. Thanks to http://www.codeproject.com/Answers/998872/Display-XML-in-HTML-Div#answer1
这对我有用。感谢http://www.codeproject.com/Answers/998872/Display-XML-in-HTML-Div#answer1
回答by Jacob
If you treat the content as text, not HTML, then DOM operations should cause the data to be properly encoded. Here's how you'd do it in jQuery:
如果您将内容视为text而不是HTML,那么 DOM 操作应该会导致数据正确编码。以下是您在 jQuery 中的做法:
$('#container').text(xmlString);
Here's how you'd do it with standard DOM methods:
以下是使用标准 DOM 方法的方法:
document.getElementById('container')
.appendChild(document.createTextNode(xmlString));
If you're placing the XML inside of HTML through server-side scripting, there are bound to be encoding functions to allow you to do that (if you add what your server-side technology is, we can give you specific examples of how you'd do it).
如果您通过服务器端脚本将 XML 放置在 HTML 中,那么肯定会有编码功能允许您这样做(如果您添加您的服务器端技术,我们可以为您提供具体示例会做的)。
回答by mkirouac
2017 Update I guess. textarea worked fine for me using Spring, Bootstrap and a bunch of other things. Got the SOAP payload stored in a DB, read by Spring and push via Spring-MVC. xmp didn't work at all.
2017 更新我猜。textarea 使用 Spring、Bootstrap 和其他一些东西对我来说效果很好。将 SOAP 负载存储在数据库中,由 Spring 读取并通过 Spring-MVC 推送。xmp 根本不起作用。