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


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


XMLReader::moveToNextAttribute()函數是PHP中的內置函數,如果位於屬性上,則用於將光標移至下一個屬性;如果位於元素上,則用於將光標移至第一個屬性。此函數還可用於檢查元素中是否存在屬性。

用法:

bool XMLReader::moveToNextAttribute( void )

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



返回值:如果成功,此函數將返回TRUE,否則將返回FALSE。

以下示例說明了PHP中的XMLReader::moveToNextAttribute()函數:

範例1:

  • data.xml
    <?xml version="1.0" encoding="utf-8"?> 
    <div> 
        <h1> Foo Bar </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 
    // to reach the h1 node 
    $XMLReader->read(); 
    $XMLReader->read(); 
    $XMLReader->read(); 
      
    // Checking if attribute is there or not 
    if ($XMLReader->moveToNextAttribute()) { 
        echo "Attribute is there"; 
    } else { 
        echo "No, attributes."; 
    } 
    ?>
  • 輸出:
    No, attributes.

範例2:

  • data.xml
    <?xml version="1.0" encoding="utf-8"?> 
    <div> 
        <h1 attrib1="value1"
            attrib2="value2" 
            attrib3="value"> 
         Foo Bar  
        </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 
    // to reach the h1 node 
    $XMLReader->read(); 
    $XMLReader->read(); 
    $XMLReader->read(); 
      
    // Move to first attribute 
    $XMLReader->moveToFirstAttribute(); 
      
    // Print name of element 
    echo "Before:<br> We are currently "
          . "at:$XMLReader->name<br>"; 
      
    // Move to next attribute 
    $XMLReader->moveToNextAttribute(); 
      
    // Print name of element 
    echo "After:<br> We are currently "
          . "at:$XMLReader->name"; 
    ?>
  • 輸出:
    Before:
    We are currently at:attrib1
    After:
    We are currently at:attrib2

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




相關用法


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