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
)
)
完整的PHP SimpleXML參考手冊