Application examples of XPath for Php+xml programming, XPath application example
The application of XPath in Php+xml programming is described in this paper. Share to everyone for your reference. Specific as follows:
The core idea of XPath design: quickly navigate to the element (or node) you need. After the PHP file is loaded into the XML file and the DOMDocument object is created, you can begin building the Domxpath object. The form of establishment is as follows:
Copy the Code code as follows: $xpath = new Domxpath ($xmldoc);
Once the Domxpath object is established, you can start using the Domxpath::query () method to find the element you want:
Copy the code as follows: $item = $xpath->query ("XPath path expression");//The return value is Domnodlist object
Instance:
XML Document: Words.xml
Copy the Code code as follows: <?xml version= "1.0" encoding= "Utf-8"?>
Boy
Boy
Girl
Girl
Teacher
Teacher
Beauty
Beauty
XPath application: index.php
Copy the Code code as follows: <?php
$xmldoc = new DOMDocument ();
Loading files
$xmldoc->load ("Words.xml");
Querying with XPath
$xpath = new Domxpath ($xmldoc);//Create Domxpath Object
$node _list = $xpath->query ("/words/word/ch");//Query ch This element, return value is Domnodelist object
echo $node _list->item (0)->nodevalue;
?>
It is hoped that this article is helpful to everyone's php+xml program design.
http://www.bkjia.com/PHPjc/947221.html www.bkjia.com true http://www.bkjia.com/PHPjc/947221.html techarticle Application examples of XPath for Php+xml programming, examples of XPath application in this paper, the application of XPath in Php+xml programming is described. Share to everyone for your reference. As follows: The core of XPath design ...