Search code examples
phpxmlfilebrowsernew-operator

How to display raw xml file in php to web browser


Hi i got an xml file and i want to display it on a website. I tried everything what i found and tried to do it my self but im a newbie to any code language. My code is this right now:

<?php
header('Content-type: text/xml');

    $xml = simplexml_load_file(xmlfile.xml) ;

echo $xml ;



?>

And the output what i see when i go to my website is nothing just a warning about: This XML file does not appear to have any style information associated with it. The document tree is shown below.

But i cant see anything. So please can someone help me write a code that outputs the whole xml file including xml declaration , tags and node values?


Solution

  • You do not have to use the simplexml_load_file: there is another function to read a file, this function is file_get_contents($filename).
    Here is a simple code to use:

    <?php
    // Set the encoding to XML
    header('Content-type: text/xml');
    
    // Get contents of the file
    $xml = file_get_contents("xmlfile.xml") ;
    
    // Print contents
    echo $xml;
    
    ?>
    

    I hope it helped you! And sorry for the language mistakes ;)