Reputation: 13683
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;
?>
Upvotes: 3
Views: 8463
Reputation: 8191
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;
Upvotes: 2
Reputation: 10513
When you go to load XML data, there's two ways to do it. You either load the contents of the XML file as a string, then pass that string to Simple XML:
$fileContents = file_get_contents('test.xml'); # reads the file and returns the string
$xml = simplexml_load_string($fileContents); # creates a Simple XML object from a string
print_r($xml); # output is a Simple XML object
...or, you load the file directly into a Simple XML Object:
$xml = simplexml_load_file('test.xml'); # Instantiates a new Simple XML object from the file, without you having to open and pass the string yourself
print_r($xml); # output is a Simple XML object
References: https://www.php.net/manual/en/function.simplexml-load-file.php
https://www.php.net/manual/en/function.simplexml-load-string.php
Upvotes: 2