PHP xpath() 函數

定義和用法
xpath() 函數運行對 XML 文檔的 XPath 查詢。
如果成功,該函數返回 SimpleXMLElements 對象的一個數組。如果失敗,則返回 FALSE。
語法
class SimpleXMLElement
{
string xpath(path)
}
{
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>
<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);
?>
$xml = simplexml_load_file("test.xml");
$result = $xml->xpath("from");
print_r($result);
?>
上面的代碼將輸出:
Array
(
[0] => SimpleXMLElement Object
(
[0] => Jani
)
)
(
[0] => SimpleXMLElement Object
(
[0] => Jani
)
)
