在HTML中嵌入XML [英] Embedding XML in HTML
问题描述
我在考虑使用CDDATA是最好的方法,但在HTML文档中出现错误时
< script><![CDATA [...]]>< / script>
我需要嵌入一个XML文档以便稍后使用JavaScript获取。我需要这样做,因为当用户打开它时,它们可能无法访问互联网。
只要XML不会在任何地方都不包含< / script>
,您可以使用自定义的类型
属性将其放入脚本标记中并没有CDATA部分)。为脚本标记添加一个 id
属性,以便获取内容。
< script id =myxmltype =text / xmldata>
< x>
< y z =foo>
< / y>
< / x>
< / script>
...
< script> alert(document.getElementById('myxml')。innerHTML);< / script>
How would one go about embedding XML in a HTML page?
I was thinking using CDDATA would be the best approach but I get errors in the HTML document when the page loads.
<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.
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.
<script id="myxml" type="text/xmldata">
<x>
<y z="foo">
</y>
</x>
</script>
...
<script> alert(document.getElementById('myxml').innerHTML); </script>
这篇关于在HTML中嵌入XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!