當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP SimpleXMLElement XPath()用法及代碼示例


前提條件: 閱讀XML基礎

SimpleXMLElement::xpath()函數是PHP中的一個內置函數,該函數在XML文檔上運行XPath查詢。

用法:


SimpleXMLElement::xpath( $path )

參數:該函數接受必需的單個參數$path。它用於指定XML文檔的XPath路徑。

返回值:如果成功,則返回SimpleXMLElements數組;如果失敗,則返回False。

注意:此函數在PHP 5.2.0和更高版本上可用。

例:

<?php 
  
// Loading XML document to $user 
$user = <<<XML 
<user> 
    <id>12345</id> 
    <username>Geeks123</username> 
    <name>GeeksforGeeks</name> 
    <phone>+91-XXXXXXXXXX</phone> 
    <detail font-color="blue" font-size="24px"> 
        Noida India 
    </detail> 
</user> 
XML; 
  
// Loading string as simple xml object 
$xml = simplexml_load_string($user); 
  
// Retrieving xpaths 
$result = $xml->xpath("username"); 
  
// Printing output 
print_r($result); 
   
?>
輸出:
Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => Geeks123
        )

)

範例2:

<?php 
  
// Loading XML document to $user 
$user = <<<XML 
<user xmlns:user_id="http://geeksforgeeks.org/user"> 
<single_user id="1"> 
    <user_id:id>12345</user_id:id> 
    <username>Geeks123</username> 
    <name>GeeksforGeeks</name> 
    <phone>+91-XXXXXXXXXX</phone> 
    <detail font-color="blue" font-size="24px"> 
        Noida India 
    </detail> 
</single_user> 
  
<single_user id="2"> 
    <user_id:id>15980</user_id:id> 
    <username>Geeks54321</username> 
    <name>Geeks</name> 
    <phone>+91-XXXXXXXXXX</phone> 
    <detail font-color="blue" font-size="24px"> 
        Noida India 
    </detail> 
</single_user> 
</user> 
XML; 
  
// Loading string as simple xml object 
$xml = simplexml_load_string($user); 
  
// Registering xpath namespace 
$xml->registerXPathNamespace('u', 'http://geeksforgeeks.org/user'); 
  
// Retrieving xpaths 
$result = $xml->xpath('//u:id'); 
  
// Printing output 
foreach ($result as $id) { 
    echo $id . "<br>"; 
} 
  
?>
輸出:
12345
15980

範例3:

<?php 
  
// Loading XML document to $user 
$user = <<<XML 
<user xmlns:user_id="http://geeksforgeeks.org/user"> 
    <single_user id="1" xmlns:name="http://geeksforgeeks.org/user/name"> 
        <user_id:id>12345</user_id:id> 
        <username>rakesh123</username> 
        <name:firstname>Rakesh</name:firstname> 
        <name:lastname>Kumar</name:lastname> 
        <phone>+91-XXXXXXXXXX</phone> 
        <detail>Noida India</detail> 
    </single_user> 
      
    <single_user id="2" xmlns:name="http://geeksforgeeks.org/user/name"> 
        <user_id:id>57833</user_id:id> 
        <username>man123</username> 
        <name:firstname>Manjeet</name:firstname> 
        <name:lastname>Singh</name:lastname> 
        <phone>+91-XXXXXXXXXX</phone> 
        <detail>Kolkata, India</detail> 
    </single_user> 
      
    <single_user id="3" xmlns:name="http://geeksforgeeks.org/user/name"> 
        <user_id:id>98944</user_id:id> 
        <username>ak98</username> 
        <name:firstname>Ak</name:firstname> 
        <name:lastname>Singh</name:lastname> 
        <phone>+91-XXXXXXXXXX</phone> 
        <detail>Noida India</detail> 
    </single_user> 
</user> 
XML; 
  
// Loading string as simple xml object 
$xml = simplexml_load_string($user); 
  
// Registering xpath namespace 
$xml->registerXPathNamespace('u', 'http://geeksforgeeks.org/user'); 
$xml->registerXPathNamespace('un', 'http://geeksforgeeks.org/user/name'); 
  
// Retrieving xpaths 
$result = $xml->xpath('//u:id'); 
$result_f_name = $xml->xpath('//un:firstname'); 
$result_l_name = $xml->xpath('//un:lastname'); 
  
// Printing output 
foreach ($result as $id) { 
    echo $id . "<br>"; 
} 
     
foreach ($result_f_name as $f_name) { 
    echo $f_name . "<br>"; 
} 
  
foreach ($result_l_name as $l_name) { 
    echo $l_name . "<br>"; 
} 
  
?>
輸出:
12345
57833
98944
Rakesh
Manjeet
Ak
Kumar
Singh
Singh

參考: https://www.php.net/manual/en/simplexmlelement.xpath.php



相關用法


注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 PHP | SimpleXMLElement XPath() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。