PHP Conference Japan 2024

XMLReader::getAttribute

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

XMLReader::getAttribute取得指定屬性的值

說明

public XMLReader::getAttribute(字串 $name): ?字串

傳回指定屬性的值,如果屬性不存在或未定位在元素節點上,則傳回 null

參數

name

屬性的名稱。

傳回值

屬性的值,如果找不到具有給定 name 的屬性或未定位在元素節點上,則為 null

更新日誌

版本 說明
8.0.0 此函數不再返回 false

另請參閱

新增筆記

使用者貢獻的筆記 3 則筆記

匿名
13 年前
或者使用 XMLReader::ELEMENT 和 XMLReader::END_ELEMENT,而不是數字 1 和 15。
Gabi
17 年前
可能很明顯,但並非所有人都知道 ;-) ... 從具有子節點的節點讀取屬性(並從此節點創建輸出)時,輸出將發出兩次,一次在 <ELEMENT> 標籤上,一次在結束標籤 </ELEMENT> 上。為避免這種情況,您可以使用屬性 nodeType 測試您位於節點的哪個部分。
元素為 1,結束元素為 15。
sven at e7o dot de
1 年前
要取得所有屬性及其名稱,請使用如下程式碼

<?php
function getAttributes($reader)
{
$attributes = [];
for (
$i = 0; $i < $reader->attributeCount; $i++) {
$reader->moveToAttributeNo($i);
$attributes[$reader->name] = $reader->value;
}
return
$attributes;
}
?>
To Top