Reputation: 860
I have a problem that has gotten me scratching my head and I'm sure it's a simple solution but for the life of me, I can't find it.
I'm trying to count the nodes of an XML file using XMLReader in PHP. Nothing new until now and there are plenty of examples here on how to do that. The problem is that all examples are loading from a string where the whole XML file is represented (simplexml_load_string). That works fine. However, if I try to load from a file I get the "Start tag expected, '<' not found" exception for SimpleXML.
Here's the code:
$xml = simplexml_load_file($fname); //$fname is a valid xml file, it loads fine
$elem = new SimpleXMLElement($xml); //it chokes on this line
Loading it from a string works though but I do want to load it from a file instead:
$xml = simplexml_load_string($xmlstring); //$xmlstring is the content of the same xml file
What am I missing? Thanks for any help.
Upvotes: 1
Views: 4252
Reputation: 1331
simplexml_load_file Interprets an XML file into an xml object thus $xml is itself a xml object, to access it`s elements just use $xml object properties no need to create the new object $elm
simplexml_load_file returns an object of class SimpleXMLElement with properties containing the data held within the XML document, or FALSE on failure.
http://php.net/manual/en/function.simplexml-load-file.php
Upvotes: 5