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


PHP XMLReader moveToAttribute()用法及代碼示例

XMLReader::moveToAttribute()函數是PHP中的內置函數,用於將光標移動到命名屬性。

用法:

bool XMLReader::moveToAttribute( string $name )

參數:該函數接受單個參數$name,該參數保存屬性的名稱。



返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。

下麵給出的程序說明了PHP中的XMLReader::moveToAttribute()函數:

程序1:
文檔名稱: data.xml

<?xml version="1.0" encoding="utf-8"?> 
<div> 
    <h1> GeeksforGeeks </h1> 
</div>

文檔名稱: index.php

<?php 
  
// Create a new XMLReader instance 
$XMLReader = new XMLReader(); 
  
// Open the XML file 
$XMLReader->open('data.xml'); 
  
// Iterate through the XML nodes 
while ($XMLReader->read()) { 
    if ($XMLReader->nodeType == XMLREADER::ELEMENT) { 
  
        // Move to attribute of name "class" 
        $XMLReader->moveToAttribute("class"); 
  
        // Output the value to browser 
        echo $XMLReader->value; 
    } 
} 
?>

輸出:

// Empty string because there is no attribute with given name.

程序2:
文檔名稱: data.xml

<?xml version="1.0" encoding="utf-8"?> 
<div> 
    <h1 attrib="value"> My Text </h1> 
</div>

文檔名稱: index.php

<?php 
// Create a new XMLReader instance 
$XMLReader = new XMLReader(); 
  
// Open the XML file 
$XMLReader->open('data.xml'); 
  
// Iterate through the XML nodes 
while ($XMLReader->read()) { 
    if ($XMLReader->nodeType == XMLREADER::ELEMENT) { 
  
        // Move to attribute of name "attrib" 
        $XMLReader->moveToAttribute("attrib"); 
  
        // Output the value to browser 
        echo $XMLReader->value; 
    } 
} 
?>

輸出:

value

參考: https://www.php.net/manual/en/xmlreader.movetoattribute.php




相關用法


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