ExampleExample Org"; $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $links = $xpath->query("//a"); foreach ($links as $link) { echo $link->getAttribute("href") . "\n"; } ?>
In this example, we create a new DOMDocument object and load an XML string into it using the loadXML method. We then create a new DOMXPath object and use the query method to find all the "book" elements in the document. We loop through the resulting NodeList and use the query method with an XPath expression to find the "title" and "author" child elements of each "book" element. We then use the nodeValue property to extract the text content of each element. The package library for DOMXPath is the PHP DOM extension, which is part of the core PHP language.The Great Gatsby F. Scott Fitzgerald '; $doc = new DOMDocument(); $doc->loadXML($xml); $xpath = new DOMXPath($doc); $books = $xpath->query("//book"); foreach ($books as $book) { $title = $xpath->query("title", $book)->item(0)->nodeValue; $author = $xpath->query("author", $book)->item(0)->nodeValue; echo $title . " by " . $author . "\n"; } ?> To Kill a Mockingbird Harper Lee