phpdomdocumentdomxpath

Extract node information from external URL using query


I want to be able to extract information from specific nodes from an external XML file. I currently have been trying

$contents = file_get_contents('https://experiencehermann.com/post-sitemap.xml');
$dom = new DOMDocument;
$dom -> loadXML($contents);
$finder = new DOMXPath($dom);
$nodes = $finder->query('//loc');
foreach ($nodes as $node) {
    echo $node->nodeValue ."</br />";
}

I'm able to use this same technique when I have the XML in the PHP directly but not when pulling from an external source.

Thanks in advance!


Solution

  • As your query is quite simple, you don't even need XPath, you can simply use the getElementsByTagName method on the DOMDocument object:

    $dom = new DOMDocument;
    $dom->loadXML($contents);
    
    $nodes = $dom->getElementsByTagName('loc');
    
    foreach ($nodes as $node) {
        if ($node->nodeName === 'image:loc')
            continue;
        echo $node->nodeValue ."<br />\n";
    }