Javascript 在 HTML 中嵌入 XML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12712101/
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
Embedding XML in HTML
提问by amcdnl
How would one go about embedding XML in a HTML page?
如何在 HTML 页面中嵌入 XML?
I was thinking using CDDATA would be the best approach but I get errors in the HTML document when the page loads.
我认为使用 CDDATA 将是最好的方法,但是当页面加载时我在 HTML 文档中出现错误。
<script><![CDATA[ ... ]]></script>
I'm needing to embed a XML document for fetching later with JavaScript. I need to do this since when the user opens it, they might not have internet access.
我需要嵌入一个 XML 文档,以便稍后使用 JavaScript 获取。我需要这样做,因为当用户打开它时,他们可能无法访问互联网。
回答by Dagg Nabbit
As long as the XML doesn't contain </script>
anywhere, you can put it inside the script tags with a custom type
attribute (and no CDATA section). Give the script tag an id
attribute so you can fetch the content.
只要 XML 不包含</script>
任何地方,您就可以将它放在带有自定义type
属性的脚本标记中(并且没有 CDATA 部分)。为脚本标签提供一个id
属性,以便您可以获取内容。
<script id="myxml" type="text/xmldata">
<x>
<y z="foo">
</y>
</x>
</script>?
...
<script> alert(document.getElementById('myxml').innerHTML);? </script>
回答by Kamiel Wanrooij
How about:
怎么样:
<script>
var xml = '<element> \
<childElement attr="value" /> \
</element>';
</script>
That would enable you to easily embed the XML for later retrieval in javascript.
这将使您能够轻松地将 XML 嵌入到 javascript 中以便以后检索。
回答by Chong Lip Phang
According to the tutorial here, you can use the 'xml' tag to embed XML data within an HTML document. However, this implicitly displays the XML data in the browser.
根据此处的教程,您可以使用“xml”标签在 HTML 文档中嵌入 XML 数据。但是,这会在浏览器中隐式显示 XML 数据。
http://www.expertrating.com/courseware/XMLCourse/XML-Embedding-HTML-8.asp
http://www.expertrating.com/courseware/XMLCourse/XML-Embedding-HTML-8.asp