Uploading a file using simplexml_load_file

I'm puzzled why this doesn't work yet. I can repeat test.xml

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

$movies = new SimpleXMLElement($xml);

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



source to share

2 answers

You don't need to do both simplexml_load_file

and create a new object SimpleXML



already interprets the XML file in the object. (Mind you, it doesn't accept an XML string)

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


Alternatively, you can directly load the XML string into an object SimpleXML


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


Any of the above approaches can be used to do the following:

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




When you load XML data, there are two ways to do it. You either load the content of the XML file as a string and 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


Literature: http://us2.php.net/manual/en/function.simplexml-load-file.php




All Articles