XMLReader::moveToFirstAttribute()函数是PHP中的内置函数,用于将光标置于元素的第一个属性上。当我们对一个元素有多个属性并且想要获取第一个属性时,或者当我们要检查一个元素是否具有任何属性时,此函数很有用。
用法:
bool XMLReader::moveToFirstAttribute( void )
参数:此函数不接受任何参数。
返回值:如果成功,此函数将返回TRUE,否则将返回FALSE。
以下示例说明了PHP中的XMLReader::moveToFirstAttribute()函数:
范例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->moveToFirstAttribute()) { 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 attribute with name attrib3 $XMLReader->moveToAttribute("attrib3"); // Print name of element echo "Before:<br> We are currently " . "at:$XMLReader->name<br>"; // Move to first attribute $XMLReader->moveToFirstAttribute(); // Print name of element echo "After:<br> We are currently " . "at:$XMLReader->name"; ?>
- 输出:
Before: We are currently at:attrib3 After: We are currently at:attrib1
参考: https://www.php.net/manual/en/xmlreader.movetofirstattribute.php
相关用法
- PHP XMLReader XML()用法及代码示例
- PHP XMLReader getAttributeNs()用法及代码示例
- PHP XMLReader close()用法及代码示例
- PHP XMLReader open()用法及代码示例
- PHP XMLReader getParserProperty()用法及代码示例
- PHP XMLReader moveToNextAttribute()用法及代码示例
- PHP XMLReader readString()用法及代码示例
- PHP XMLReader readInnerXml()用法及代码示例
- PHP XMLReader readOuterXml()用法及代码示例
- PHP XMLReader setRelaxNGSchema()用法及代码示例
- PHP XMLReader setParserProperty()用法及代码示例
- PHP XMLReader setRelaxNGSchemaSource()用法及代码示例
- PHP XMLReader setSchema()用法及代码示例
注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | XMLReader moveToFirstAttribute() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。