PHP xpath() 函数

定义和用法

xpath() 函数运行对 XML 文档的 XPath 查询。高佣联盟 www.cgewang.com

如果成功,该函数返回 SimpleXMLElements 对象的一个数组。如果失败,则返回 FALSE。


语法

class SimpleXMLElement
{
string xpath(path)
}
参数描述
path 必需。规定要在 XML 文档中搜索的 XPath 路径。

实例

XML 文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

PHP 代码

<?php
$xml = simplexml_load_file("test.xml");

$result = $xml->xpath("from");

print_r($result);
?>

上面的代码将输出:

Array
(
[0] => SimpleXMLElement Object
(
[0] => Jani
)
)
原文地址:https://www.cnblogs.com/yc10086/p/13112028.html