As the question says, it just escaped my memory how to display xml in javascript, I want to display some source xml within an div on a page, that sits next to the processed result of the xml in another div.
Can't remember if there was an equivalent to javascript's escape to convert entities on the client
Note: the xml files are served as is from the server, so I need a client side solution
Note: the main problem is XML doesn't render correctly in most browsers, all the brackets and attributes disappear, leaving you with text that doesn't look like xml
With a few lines of JavaScript code, you can read an XML file and update the data content of any HTML page.
If all you need to do is view the data in an XML file, you're in luck. Just about every browser can open an XML file. In Chrome, just open a new tab and drag the XML file over. Alternatively, right click on the XML file and hover over "Open with" then click "Chrome".
You can use Microsoft® Internet Explorer to view XML documents in the browser, just as you would view HTML pages. Unlike HTML, XML does not predefine display properties for specific elements.
Fetch your XML using Ajax and simply put the result in a textarea
. Style the textarea any way you want.
If you want the browser to render your XML
as XML
, it must be in it's own document, like an iframe
, or a frame
. DIV
won't do the job!
Besides, in the HTTP
header of the request that serves the iframe
you should send Content-Type: text/xml
, so the Browser can understand that this content is an XML
.
But, if you truely need to see it in a DIV you will have to build yourself an XML
rendering function XML2HTML
. You can use the HTML
PRE
tag for that, if your XML
string is already formatted (line breaks and tabs). In that case you will have to replace <
to >
in the XML
string.
Conclusion: The browser
can't render XML
and HTML
in the same document. If you need it, you just have to workaround it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With