Xpath application examples of php+xml programming, xpath application examples
The example in this article describes the application of xpath in php+xml programming. Share it with everyone for your reference. The details are as follows:
The core idea of xpath design: quickly locate the element (or node) you need. After the PHP file loads the xml file and creates the DOMDocument object, it can start to create the DOMXPath object. The establishment form is as follows:
Copy code The code is as follows:
$xpath = new DOMXPath($xmldoc);
After creating the DOMXPath object, you can start using the DOMXPath::query() method to find the elements you need:
Copy code The code is as follows:
$item = $xpath->query("xpath path expression");//The return value is DOMNodList Object
Example:
xml document: words.xml
Copy code The code is as follows:
boy
Boy
girl
Girl
teacher
Teacher
beauty
Beauty
xpath application: index.php
Copy code The code is as follows:
$xmldoc = new DOMDocument();
//Load file
$xmldoc->load("words.xml");
//Use xpath to query
$xpath = new DOMXPath($xmldoc);//Create DOMXPath object
$node_list = $xpath->query("/words/word/ch");//Query the ch element, the return value is a DOMNodeList object
echo $node_list->item(0)->nodeValue;
?>
I hope this article will be helpful to everyone’s php+XML programming.
http://www.bkjia.com/PHPjc/947221.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947221.htmlTechArticleApplication examples of xpath in php+xml programming, xpath application examples This article tells the example of xpath in php+xml programming application. Share it with everyone for your reference. The details are as follows: The core idea of xpath design...