SimpleXMLElement::attributes

(PHP 5, PHP 7, PHP 8)

SimpleXMLElement::attributesIdentifies an element's attributes

說明

public SimpleXMLElement::attributes(?string $namespaceOrPrefix = null, bool $isPrefix = false): ?SimpleXMLElement

This function provides the attributes and values defined within an xml tag.

注意: SimpleXML 建起了一個(gè)給大多數(shù)方法添加迭代屬性的規(guī)則。不能通過使用 var_dump() 或任何可檢查對象的其它東西來查看。

參數(shù)

namespaceOrPrefix

An optional namespace for the retrieved attributes

isPrefix

Default to false

返回值

Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.

Returns null if called on a SimpleXMLElement object that already represents an attribute and not a tag.

范例

示例 #1 Interpret an XML string

<?php
$string 
= <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml simplexml_load_string($string);
foreach(
$xml->foo[0]->attributes() as $a => $b) {
    echo 
$a,'="',$b,"\"\n";
}
?>

以上例程會(huì)輸出:

name="one"
game="lonely"