PHP file_get_contents() and XML files [duplicate]

<div class="question-status question-originals-of-duplicate">

This question already has an answer here:

    <li> How to display XML in HTML in PHP? <span class="question-originals-answer-count"> 5 answers </span> </li> </ul>

    in PHP I want to load a XML file (as a text file) and show its content (as a text) on a screen. I have a simple XML in the form

    <root> <parent>Parent text. </parent></root>

    If I use

    $myxmlfilecontent = file_get_contents('./myfile.xml'); echo $myfilecontent;

    prints only the content of the node "parent", it prints only "Parent text.", not the whole file content.


    When you print XML in an HTML page, the XML is assimilated to HTML, so you do not see the tags.

    To see the tags as text, you should replace them with the HTML corresponding entity:

    $myxmlfilecontent = file_get_contents('./myfile.xml'); echo str_replace('<', '&lt;', $myxmlfilecontent);

    that should do the trick

    I recommend you to also enclose the xml into a 'pre' to preserve spaces for presentation

    $myxmlfilecontent = file_get_contents('./myfile.xml'); echo '<pre>' . str_replace('<', '&lt;', $myxmlfilecontent) . '</pre>';


    It is printing the whole thing (if you look at the source of the page).

    But if the file type is set as HTML, then you will not see the nodes.


    You need to tell your browser that the content you send to it (you "echo" it to the browser) is XML. This is done by sending the proper Content-Type header:

    header('Content-Type: text/xml'); $myxmlfilecontent = file_get_contents('./myfile.xml'); echo $myxmlfilecontent;

    You browser will then try to display the XML as best as possible, normally with syntax-highlighting and controls to open and collapse nodes.

    Otherwise, by default your browser will try to display the text as HTML and because all those tags are not valid HTML tags, they are hidden. That is the default behavior of a browser.


    Add following snipet before any output:

    header("Content-Type: text/plain");

    This will force user agent (browser) to treat your output as plain text.

    On other hand, you can use some syntax highlighter like discussed here : PHP code to syntax-format XML content in a `pre` tag


  • PHP not converting JSON using 'json_decode()'
  • Zipping the contents of a folders in PHP
  • How to access list of email accounts with cPanel API?
  • How to write xml into a file using MarkupBuilder
  • Recursion Control flow
  • How to check disabled jobs with Jenkins server?
  • Android NFC read Tags issue. Activity starts each time on data received
  • Android - VerifyError
  • Phonegap GA plugin not working
  • how to ignore xml namespaces?
  • Activity overlaying. WindowManager.LayoutParams
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • PHP Listener Script for Paypal Webhooks
  • Splash Screen will not display
  • Doctrine/Symfony entity generator and generating entity from one table
  • Xamarin Android | Layout style
  • Setting the run time properties on SpringApplicationBuilder()
  • In-place sed command not working
  • Simple linked list-C
  • Enabling DTD support in Sql Server
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • How to get current document uri in XSLT?
  • CakePHP ACL tutorial initDB function warnings
  • Appending Character to Character Array In C
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Xcode 4 NSLog Macro link in Xcode 3
  • Play WS (2.2.1): post/put large request
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Deserializing XML into class C#
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • align graphs with different xlab
  • Return words with double consecutive letters
  • How to pass list parameters for each object using Spring MVC?
  • Is there a mandatory requirement to switch app.yaml?
  • Reading document lines to the user (python)
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • Python/Django TangoWithDjango Models and Databases