Application examples of XPath for Php+xml programming, XPath application example _php tutorial

Source: Internet
Author: User
Tags xpath

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.