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
相關用法
- PHP XMLReader XML()用法及代碼示例
- PHP XMLReader getAttributeNs()用法及代碼示例
- PHP XMLReader close()用法及代碼示例
- PHP XMLReader open()用法及代碼示例
- PHP XMLReader getParserProperty()用法及代碼示例
- PHP XMLReader moveToFirstAttribute()用法及代碼示例
- PHP XMLReader readString()用法及代碼示例
- PHP XMLReader readInnerXml()用法及代碼示例
- PHP XMLReader readOuterXml()用法及代碼示例
- PHP XMLReader setRelaxNGSchema()用法及代碼示例
- PHP XMLReader setParserProperty()用法及代碼示例
- PHP XMLReader setRelaxNGSchemaSource()用法及代碼示例
- PHP XMLReader setSchema()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLReader moveToNextAttribute() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。