首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

SimpleXMLElement::xpath

(PHP 5 >= 5.2.0, PHP 7)

SimpleXMLElement :: xpath - 对XML数据运行XPath查询

描述

代码语言:javascript
复制
public array SimpleXMLElement::xpath ( string $path )

XPath的方法搜索子元素匹配XPath的SimpleXML的节点path

参数

path

XPath路径

返回值

返回一个SimpleXMLElement对象数组,如果发生错误,则返回FALSE。

例子

示例#1 Xpath

代码语言:javascript
复制
<?php
$string?=?<<<XML
<a>
?<b>
??<c>text</c>
??<c>stuff</c>
?</b>
?<d>
??<c>code</c>
?</d>
</a>
XML;

$xml?=?new?SimpleXMLElement($string);

/*?Search?for?<a><b><c>?*/
$result?=?$xml->xpath('/a/b/c');

while(list(?,?$node)?=?each($result))?{
????echo?'/a/b/c:?',$node,"\n";
}

/*?Relative?paths?also?work...?*/
$result?=?$xml->xpath('b/c');

while(list(?,?$node)?=?each($result))?{
????echo?'b/c:?',$node,"\n";
}
?>

上面的例子将输出:

代码语言:javascript
复制
/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff

注意:两个结果是相等的。

扩展内容

  • SimpleXMLElement :: registerXPathNamespace() - 为下一个XPath查询创建前缀/ ns上下文
  • SimpleXMLElement :: getDocNamespaces() - 返回在文档中声明的名称空间
  • SimpleXMLElement :: getNamespaces() - 返回文档中使用的名称空间

← SimpleXMLElement::__toString

SimpleXMLIterator →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com