Loading file with simplexml_load_file

 

Questions


I am puzzled why this is not working yet i can echo the test.xml

<?php
$xml = simplexml_load_file('test.xml');

$movies = new SimpleXMLElement($xml);

echo $movies->movie[1]->plot;
?> 

 

 

————————————————-

Answer

There is no need to do both, simplexml_load_file and create a new SimpleXML object.

simplexml_load_file already interprets an XML file into an object. (Keep in mind, it does not accept an XML string)

$movies = simplexml_load_file('test.xml');

Alternatively, you may directly load an XML string into a SimpleXML object.

$movies = new SimpleXMLElement(file_get_contents('test.xml'));

Either of the above approaches can be used to execute the following:

echo $movies->movie[0]->plot;

php,simplexml

Facebook Comments

Post a comment