I’m parsing XML results from an API call using PHP and xpath.
$dom = new DOMDocument(); $dom->loadXML($response->getBody()); $xpath = new DOMXPath($dom); $xpath->registerNamespace('a', 'http://www.example.com'); $hrefs = $xpath->query('//a:Books/text()', $dom); for ($i = 0; $i < $hrefs->length; $i++) { $arrBookTitle[$i] = $hrefs->item($i)->data; } $hrefs = $xpath->query('//a:Books', $dom); for ($i = 0; $i < $hrefs->length; $i++) { $arrBookDewey[$i] = $hrefs->item($i)->getAttribute('DeweyDecimal'); }
This works but is there a way I can access both the text and the attribute from one query? And if so how do you get to those items once query is executed?
After doing some looking around I came across this solution. This way I can get the element text and access any attributes of the node.