Home  >  Article  >  Backend Development  >  Application examples of xpath in php+xml programming, application examples of xpath_PHP tutorial

Application examples of xpath in php+xml programming, application examples of xpath_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:09:05858browse

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.

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn