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


PHP DOMNode getNodePath()用法及代碼示例

DOMNode::getNodePath()函數是PHP中的內置函數,用於獲取節點的XPath位置路徑。

用法:

string DOMNode::getNodePath( void )

參數:此函數不接受任何參數。


返回值:此函數返回包含XPath的字符串,如果出現錯誤,則返回NULL。

以下示例說明了PHP中的DOMNode::getNodePath()函數:

範例1:

<?php 
  
// Create a new DOMDocument instance 
$dom = new DOMDocument; 
  
// Load the XML 
$dom->loadXML(' 
<root> 
    <h1>GeeksforGeeks</h1> 
</root> 
'); 
  
// Print XPath of the element 
echo $dom->getElementsByTagName('h1') 
    ->item(0)->getNodePath(); 
?>

輸出:

/root/h1

範例2:

<?php 
   
// Create a new DOMDocument instance 
$dom = new DOMDocument; 
   
// Load the XML 
$dom->loadXML(' 
<root> 
    <h1>Geeks</h1> 
    <div> 
        <h1>For</h1> 
    </div> 
    <h1>Geeks</h1> 
</root> 
'); 
   
// Print XPath of the element 
for ($i = 0; $i < 3; $i++) { 
  
    // Print where the line where the 'node'  
    // element was defined in 
    echo $i+1 . ') The h1 tag\'s Xpath is:' 
        . $dom->getElementsByTagName('h1') 
        ->item($i)->getNodePath() . "<br>"; 
} 
?>

輸出:

1) The h1 tag's Xpath is:/root/h1[1]
2) The h1 tag's Xpath is:/root/div/h1
3) The h1 tag's Xpath is:/root/h1[2]

參考: https://www.php.net/manual/en/domnode.getnodepath.php



相關用法


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