SimpleXMLElement::xpath
(PHP 5 >= 5.2.0, PHP 7)
SimpleXMLElement :: xpath - 对XML数据运行XPath查询
描述
public array SimpleXMLElement::xpath ( string $path )
该XPath的方法搜索子元素匹配XPath的SimpleXML的节点path
。
参数
path
XPath路径
返回值
返回一个SimpleXMLElement对象数组,如果发生错误,则返回FALSE。
例子
示例#1 Xpath
<?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";
}
?>
上面的例子将输出:
/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 →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com