I'm writing a script to parse for this XML.
I want to parse all the <Contents>
node with DOMDocument and DOMXpath. But for some reason, all the XPath queries I tried failed.
My code:
<?php
$apiUrl = 'https://chromedriver.storage.googleapis.com/?delimiter=/&prefix=98.0.4758.48/';
$xmlContents = file_get_contents($apiUrl);
if (!$xmlDom->loadXML($xmlContents)) {
throw new \Exception('Unable to parse the chromedriver file index API response as XML.');
}
$xpath = new \DOMXPath($xmlDom);
// **I tried several $query values here**
$fileEntries = $xpath->query($query, null, false);
if (!$fileEntries instanceof \DOMNodeList) {
throw new \Exception('Failed to evaulate the xpath into node list.');
}
echo "There are {$fileEntries->length} results\n";
foreach ($fileEntries as $node) {
/** @var \DOMNode $node */
var_dump($node->nodeName);
}
XPath $query
I tried:
/ListBucketResult/Contents
/Contents
//Contents
All of these results in "There are 0 results".
If I use *
in the $query, it will list all the nodes within the <ListBucketResult>
root node:
There are 10 results
string(4) "Name"
string(6) "Prefix"
string(6) "Marker"
string(9) "Delimiter"
string(11) "IsTruncated"
string(8) "Contents"
string(8) "Contents"
string(8) "Contents"
string(8) "Contents"
string(8) "Contents"
The easy way is to filter the nodes with the nodeName
attribute. But I do want to know what went wrong with my XPath query. What did I miss?