SimpleXMLElement::attributes
PHP Manual
PHP Manual»SimpleXMLElement»SimpleXMLElement::attributes

SimpleXMLElement::attributes

(PHP 5, PHP 7, PHP 8)

SimpleXMLElement::attributesIdentifies an element's attributes

Опис

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

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

Зауваження: SimpleXML створив правило додавання ітеративних властивостей до більшості методів. Їх не можна побачити за допомогою var_dump() або іншими способами дослідження об'єктів.

Параметри

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";
}
?>

Поданий вище приклад виведе:

name="one"
game="lonely"

Прогляньте також

To Top