XMLReader::moveToAttributeNs()函數是PHP中的內置函數,用於將光標移動到指定名稱空間中的命名屬性上。當我們要獲取特定名稱空間中特定屬性的屬性值而忽略所有其他名稱空間時,此方法很有用。
用法:
bool XMLReader::moveToAttributeNs( string $localName, string $namespaceURI )
參數:該函數接受上述和以下所述的兩個參數:
- $localName:它指定屬性的本地名稱。
- $namespaceURI:它指定名稱空間URI。
返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。
下麵給出的程序說明了PHP中的XMLReader::moveToAttributeNs()函數:
程序1:
文檔名稱: data.xml
<?xml version="1.0" encoding="utf-8"?>
<div xmlns:z="my_namespace">
<z:h1 z:attrib="value"> Foo Bar </z: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 attrib
// with the namespace "my_namespace"
$XMLReader->moveToAttributeNs("attrib",
"wrong_namespace");
// Output the value to browser
echo $XMLReader->value;
?>
輸出:
// Empty string because there is no namespace called "wrong_namespace"
程序2:
文檔名稱: data.xml
<?xml version="1.0" encoding="utf-8"?>
<div xmlns:z="my_namespace">
<z:h1 z:attrib="value"> Foo Bar </z: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 z:h1 node
$XMLReader->read();
$XMLReader->read();
$XMLReader->read();
// Move to attribute with name attrib
// with the namespace "my_namespace"
$XMLReader->moveToAttributeNs("attrib",
"my_namespace");
// Output the value to browser
echo $XMLReader->value;
?>
輸出:
value
參考: https://www.php.net/manual/en/xmlreader.movetoattributens.php
相關用法
- PHP XMLReader XML()用法及代碼示例
- PHP XMLReader getAttribute()用法及代碼示例
- PHP XMLReader setRelaxNGSchema()用法及代碼示例
- PHP XMLReader moveToFirstAttribute()用法及代碼示例
- PHP XMLReader moveToNextAttribute()用法及代碼示例
- PHP XMLReader setRelaxNGSchemaSource()用法及代碼示例
- PHP XMLReader expand()用法及代碼示例
- PHP XMLReader setSchema()用法及代碼示例
- PHP XMLReader readInnerXml()用法及代碼示例
- PHP XMLReader open()用法及代碼示例
- PHP XMLReader close()用法及代碼示例
- PHP XMLReader readString()用法及代碼示例
- PHP XMLReader lookupNamespace()用法及代碼示例
- PHP XMLReader getAttributeNo()用法及代碼示例
- PHP XMLReader getAttributeNs()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLReader moveToAttributeNs() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。