When you want to process an XML file in PHP, there are two possibilities: either we use SimpleXML or we go through DOMDocument. In this article I will deal more specifically with SimpleXML.
To load a file, just use the simplexml_load_file method
$xml = simplexml_load_file($file);By doing so, you get a SimpleXMLElement.
If the XML has a namespace, it is imperative to add it to the element, otherwise you won’t be able to browse it.
$xml->registerXPathNamespace('n', 'http://www.w3.org/TR/html4/');
<movies xmlns="http://www.w3.org/TR/html4/">
<movie>
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
<character>
<name>Mr. Coder</name>
<actor>El ActÓr</actor>
</character>
</characters>
<plot>
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
</plot>
<great-lines>
<line>PHP solves all my web problems</line>
</great-lines>
<rating type="thumbs">7</rating>
<rating type="stars">5</rating>
</movie>
</movies>
Then it is possible to retrieve the desired data
echo $movies->movie[0]->plot;we can also, if desired, use xpath. It is very important that each node has the namespace, in our example “n”
echo $xml->xpath('//n:movie/n:plot')A very good article can be found on php.net.
