This article mainly introduces the application of xpath in php + xml programming. The example analyzes the functions, definitions, and usage skills of xpath, if you need it, you can refer to the following example to describe the application of xpath in php + xml programming. Share it with you for your reference. The details are as follows:
The core idea of xpath design: quickly locate the elements (or nodes) you need ). After the PHP file loads the xml file and creates a DOMDocument object, you can create a DOMXPath object. The procedure is as follows:
The code is as follows:
$ Xpath = new DOMXPath ($ xmldoc );
After the DOMXPath object is created, you can use the DOMXPath: query () method to find the elements you need:
The code is as follows:
$ Item = $ xpath-> query ("xpath path expression"); // The return value is a DOMNodList object.
Instance:
Xml document: words. xml
The code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
Boy
Boy
Girl
Girl
Teacher
Instructor
Beauty
Beauty
Xpath application: index. php
The code is as follows:
<? Php
$ Xmldoc = new DOMDocument ();
// Load the file
$ Xmldoc-> load ("words. xml ");
// Query using xpath
$ Xpath = new DOMXPath ($ xmldoc); // Create a DOMXPath object
$ Node_list = $ xpath-> query ("/words/word/ch"); // query the ch element. the returned value is the DOMNodeList object.
Echo $ node_list-> item (0)-> nodeValue;
?>
I hope this article will help you with php + XML programming.