Home > php教程 > php手册 > body text

php+xml编程之xpath的应用实例,xpath应用实例

WBOY
Release: 2016-06-13 09:16:34
Original
962 people have browsed it

php+xml编程之xpath的应用实例,xpath应用实例

本文实例讲述了php+xml编程之xpath的应用。分享给大家供大家参考。具体如下:

xpath设计的核心思想:迅速定位到你所需要的元素(或者节点)。PHP 文件在载入xml文件和创建DOMDocument 对象后,就可以开始建立DOMXPath 对象。建立形式如下:

复制代码 代码如下:

$xpath = new DOMXPath($xmldoc);

建立 DOMXPath 对象后,就可以开始使用 DOMXPath::query()方法,找到你需要的元素:

复制代码 代码如下:

$item = $xpath->query("xpath路径表达式");//返回值为DOMNodList对象

实例:

xml文档:words.xml

复制代码 代码如下:




 boy
 男孩


 girl
 女孩


 teacher
 老师


 beauty
 美女

xpath应用:index.php

复制代码 代码如下:

$xmldoc = new DOMDocument();
//加载文件
$xmldoc->load("words.xml");
//使用xpath查询
$xpath = new DOMXPath($xmldoc);//创建DOMXPath对象
$node_list = $xpath->query("/words/word/ch");//查询ch这个元素,返回值为DOMNodeList对象
echo $node_list->item(0)->nodeValue;
?>

希望本文所述对大家的php+XML程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template