amcdnl
amcdnl

Reputation: 8638

Embedding XML in HTML

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.

Upvotes: 28

Views: 50029

Answers (3)

Chong Lip Phang
Chong Lip Phang

Reputation: 9279

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.

http://www.expertrating.com/courseware/XMLCourse/XML-Embedding-HTML-8.asp

Upvotes: 0

Dagg Nabbit
Dagg Nabbit

Reputation: 76736

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/

Upvotes: 38

Kamiel Wanrooij
Kamiel Wanrooij

Reputation: 12404

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.

Upvotes: 0

Related Questions