在HTML中嵌入XML [英] Embedding XML in HTML

查看:289
本文介绍了在HTML中嵌入XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在考虑使用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>

http://jsfiddle.net/hJuPs/


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>

http://jsfiddle.net/hJuPs/

这篇关于在HTML中嵌入XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆