当前位置:首页 > PHP教程 > php函数 > 列表

PHP xpath()函数讲解

发布:smiling 来源: PHP粉丝网  添加日期:2021-11-07 20:00:57 浏览: 评论:0 

今天小编就为大家分享一篇关于PHP xpath()函数讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧。

PHP xpath() 函数

定义和用法

xpath()函数运行对 XML 文档的 XPath 查询。

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

语法:

  1. class SimpleXMLElement  
  2. {  
  3. string xpath(path)  

实例

XML 文件

  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <note>  
  3. <to>Tove</to>  
  4. <from>Jani</from>  
  5. <heading>Reminder</heading>  
  6. <body>Don't forget me this weekend!</body>  
  7. </note> 
  8. <?xml version="1.0" encoding="ISO-8859-1"?>  
  9. <note>  
  10. <to>Tove</to>  
  11. <from>Jani</from>  
  12. <heading>Reminder</heading>  
  13. <body>Don't forget me this weekend!</body>  
  14. </note> 

PHP 代码

  1. <?php  
  2. $xml = simplexml_load_file("test.xml");  
  3. $result = $xml->xpath("from");  
  4. print_r($result);  
  5. ?> 

上面的代码将输出:

  1. Array  
  2. (  
  3. [0] => SimpleXMLElement Object  
  4. (  
  5. [0] => Jani  
  6. )  
  7. )

Tags: xpath

分享到: