Preventing PHP from auto parsing XML

Amal Antony Source

I'm making an api call to a site using the code as shown below :

 $xmlData = file_get_contents("");

 echo $xmlData;

However xmlData when displayed on the browser is auto parsed to HTML. For e.g. The element <title> of the returned XML which is actually a book title is converted to HTML essentially becoming the page title, and the other XML elements are displayed as plain text without the tags. I want the client side XMLHttpRequest Object to get raw XML data from the server side.

Why does this happen and how do I ensure that XML is not auto parsed?



answered 6 years ago Marcin Orlowski #1

This got nothing to do with php. you spit out elements which browser interprets as HTML (that's why it sets title). Build your html page right, use <pre> tags around your content, or. when needed, send your content with correct content-type header (like text/plain to display your xml for viewing or text/xml for other purposes) so it will not parse your data as html.

answered 6 years ago Niet the Dark Absol #2

PHP just sees it as text. For instance, do echo "<b>Bold</b>"; and it will "automatically" be in bold. It is the browser that processes the HTML and renders it.

This is what htmlspecialchars is for.

comments powered by Disqus