当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。