Read XML file using PHP

0
COM
Reading XML file in PHP is as easy as copy-paste. We can read an XML file using simplexml_load_file() function which takes XML filename as a parameter:

simplexml_load_file($filename);

Let's read an XML file containing song list:



Now the PHP:


?php  
$filename = "sample.xml";
$xml = simplexml_load_file($filename);
?>

Now you access any tag in the XML using the variable $xml.
Let's see how it works: 





Now let's what it did for us:


You can also access the attributes like this: 



which will look like this:


And in the end if you want to read the whole XML, then abviously loop will be used, here is the code:



And here is the output: 




Hope this tutorial will be helpful to you...