Embedding XML in HTML

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/

Leave a Comment