Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display XML in HTML in PHP?

Tags:

html

php

xml

I have a string with XML:

$string =  " <shoes>     <shoe>        <shouename>Shoue</shouename>     </shoe> </shoes> "; 

And would like display it on my website like this:

This is XML string content: <shoes>     <shoe>        <shouename>Shoue</shouename>     </shoe> </shoes> 

So I would like to do it:

  • on site, not in textbox
  • without external libraries, frameworks etc.
  • formatted with proper new lines
  • formatted with tabs
  • without colors etc., only text

So how to do it in plain and simple way?

like image 402
Tom Smykowski Avatar asked May 19 '10 09:05

Tom Smykowski


People also ask

How do I display XML content in HTML?

Since XML tags are "invented" by the author of the XML document, browsers do not know if a tag like <table> describes an HTML table or a dining table. Without any information about how to display the data, the browsers can just display the XML document as it is. Tip: If you want to style an XML document, use XSLT.

How display XML file in browser using PHP?

php header("Content-type: text/xml"); $yourFile = "xmlfile. xml"; $file = file_get_contents($yourFile); echo $file; If you insist on simple xml you can write like this.

How can I use XML file in PHP?

PHP 5's new SimpleXML module makes parsing an XML document, well, simple. It turns an XML document into an object that provides structured access to the XML. To create a SimpleXML object from an XML document stored in a string, pass the string to simplexml_load_string( ). It returns a SimpleXML object.

Can PHP read XML?

The PHP simplexml_load_file() function is used to read XML data from a file.


1 Answers

If you just want a plain-text representation of your (pre-formatted) string, you can wrap it in HTML <pre/> tags and use htmlentities to escape the angle brackets:

<?PHP echo '<pre>', htmlentities($string), '</pre>'; ?>
like image 157
Chris Avatar answered Sep 26 '22 10:09

Chris