Php + xml Programming: An xpath application instance. Php + xml programming-an xpath application example. This document describes the application of php + xml programming-xpath. Share it with you for your reference. Specifically: The xpath application example of php + xml programming in the core thinking of xpath design, and the xpath application example
This article describes 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.
Examples in this article describes the application of xpath in php + xml programming. Share it with you for your reference. Specifically: The core idea of xpath design...