今天小編就為大家分享一篇關于PHP xpath()函數講解,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧。
PHP xpath() 函數
定義和用法
xpath()函數運行對 XML 文檔的 XPath 查詢。
如果成功,該函數返回 SimpleXMLElements 對象的一個數組,如果失敗,則返回 FALSE。
語法:
- class SimpleXMLElement
- {
- string xpath(path)
- }
實例
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>
- <?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
- )
- )
新聞熱點
疑難解答