Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display xml in javascript?

Tags:

javascript

xml

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

like image 729
Robert Gould Avatar asked Dec 08 '08 11:12

Robert Gould


People also ask

Can you use XML with JavaScript?

With a few lines of JavaScript code, you can read an XML file and update the data content of any HTML page.

How do I display an XML feed?

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".

Can you display XML?

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.


2 Answers

Fetch your XML using Ajax and simply put the result in a textarea. Style the textarea any way you want.

like image 123
Diodeus - James MacFarlane Avatar answered Oct 02 '22 22:10

Diodeus - James MacFarlane


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 &gt; 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.

like image 37
Daniel Silveira Avatar answered Oct 02 '22 21:10

Daniel Silveira