Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  26] [ 7]  / answers: 1 / hits: 46060  / 12 Years ago, wed, october 3, 2012, 12:00:00

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.


More From » html

 Answers
67

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/


[#82772] Tuesday, October 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rhiannab

Total Points: 370
Total Questions: 98
Total Answers: 100

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
;