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.
$hrefs = $xpath->query('//a:Books', $dom);
for ($i = 0; $i < $hrefs->length; $i++) {
$arrBookTitle[$i] = $hrefs->item($i)->nodeValue;
$arrBookDewey[$i] = $hrefs->item($i)->getAttribute('DeweyDecimal');
}
One single XPath expression that will select both the text nodes of "a:Books" and their "DeweyDecimal" attribute, is the following
//a:Books/text() | //a:Books/@DeweyDecimal
Do note the use of the XPath's union operator in the expression above.
Another note: try to avoid using the "//" abbreviation as it may cause the whole XML document to be traversed and thus is very expensive. It is recommended to use a more specific XPath expression (such as consisting of a chain of specific location steps) always when the structure of the XML document is known.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With