PHP, DOMdocument and xPath to spar an xml file

When we have to process a very large xml, it is unfortunately not possible to use simpleXML. Fortunately for this, we can use DOMdocument.

For how you just load the desired file and boot xpath on it with DOMXPath.

$document - new DOMDocument();
$document-load ($file);
$xpath - new DOMXPath ($document);

If the xml in question has a namespace, don’t forget to list it.

$xpath-registerNameSpace ('n', '');

Once this is done, we can start making our xPath claims.

        $elements - $xpath-query ('/n:movie');
        foreach ($elements as $element)

It is very important that in the xPath query each nodes has namespace, in our example “n”


Leave a Reply