XMLReader::moveToAttributeNo()函數是PHP中的內置函數,用於按索引將光標移動到屬性。當節點具有多個屬性但我們僅需要特定屬性時,此函數很有用。
用法:
bool XMLReader::moveToAttributeNo( int $index )
參數:該函數接受單個參數$index,該參數保存屬性的位置。
返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。
下麵給出的程序說明了PHP中的XMLReader::moveToAttributeNo()函數:
程序1:在此程序中,我們將獲取特定節點的第二個屬性的值。
文檔名稱: data.xml
<?xml version="1.0" encoding="utf-8"?>
<div>
<h1 att1="first" attr2="second"> 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
// to reach the h1 node
$XMLReader->read();
$XMLReader->read();
$XMLReader->read();
// Move to second attribute
// of current node
$XMLReader->moveToAttributeNo(1);
// Output the value to browser
echo $XMLReader->value;
?>
輸出:
second
程序2:在此程序中,我們將獲取所有節點的第一個屬性的值。
文檔名稱: data.xml
<?xml version="1.0" encoding="utf-8"?>
<div>
<h1 attribute="geeksforgeeks"> 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 first attribute
$XMLReader->moveToAttributeNo(0);
// Output the value to browser
echo $XMLReader->value;
}
}
?>
輸出:
geeksforgeeks
參考: https://www.php.net/manual/en/xmlreader.movetoattributeno.php
相關用法
- PHP XMLReader next()用法及代碼示例
- PHP XMLReader XML()用法及代碼示例
- PHP XMLReader moveToFirstAttribute()用法及代碼示例
- PHP XMLReader getParserProperty()用法及代碼示例
- PHP XMLReader close()用法及代碼示例
- PHP XMLReader isValid()用法及代碼示例
- PHP XMLReader open()用法及代碼示例
- PHP XMLReader getAttributeNo()用法及代碼示例
- PHP XMLReader read()用法及代碼示例
- PHP XMLReader expand()用法及代碼示例
- PHP XMLReader getAttributeNs()用法及代碼示例
- PHP XMLReader readInnerXml()用法及代碼示例
- PHP XMLReader setRelaxNGSchema()用法及代碼示例
- PHP XMLReader setParserProperty()用法及代碼示例
- PHP XMLReader readString()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLReader moveToAttributeNo() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。