當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP XMLReader moveToAttributeNs()用法及代碼示例

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




相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | XMLReader moveToAttributeNs() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。