-1

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?

  • Rather than convert it using `$xml = simplexml_load_file(xmlfile.xml) ;` you can just display the file using `readfile("xmlfile.xml");` – Nigel Ren Sep 13 '19 at 17:46

2 Answers2

1

Try this code. It works for me.

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

$xml = simplexml_load_file("xmlfile.xml");

echo $xml->asXML();
1

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 ;)

YaFou
  • 51
  • 6