Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding XML in HTML

Tags:

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.

like image 714
amcdnl Avatar asked Oct 03 '12 15:10

amcdnl


People also ask

How do I display XML content in HTML?

To view raw XML source, try to select "View Page Source" or "View Source" from the browser menu. Note: In Safari 5 (and earlier), only the element text will be displayed. To view the raw XML, you must right click the page and select "View Source".

What is embedded XML?

Embedded expressions enable you to create XML literals that contain expressions that are evaluated at run time. The syntax for an embedded expression is <%= expression %> , which is the same as the syntax used in ASP.NET.

How do I add an XML file to my website?

Create a folder in your website called "XMLFiles". Create a file in that folder called "Videos. xml" and place your XML in the file.


1 Answers

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/

like image 164
Dagg Nabbit Avatar answered Sep 29 '22 00:09

Dagg Nabbit