To get an attribute in the node, use node->attributes()->attributeName
SimpleXMLElement::attributes
(PHP 5 >= 5.0.1)
SimpleXMLElement::attributes — 要素の属性を定義する
説明
SimpleXMLElement attributes
([ string $ns
[, bool $is_prefix
]] )
この関数は、XMLタグの中で定義された属性とその値を取得します。
注意: SimpleXML では、ほとんどのメソッドに反復処理を追加するための手順が定義されています。 これらは、var_dump() やオブジェクトを評価する他の手段で 見ることはできません。
パラメータ
- ns
-
オプションで指定する、取得した属性の名前空間。
- is_prefix
-
デフォルトは FALSE。
返り値
例
例1 XML文字列を解釈する
<?php
$string = <<<XML
<a xmlns:b>
<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"
SimpleXMLElement::attributes
gillllberg at gmail dot com
04-Nov-2009 10:21
04-Nov-2009 10:21
skerr at mojavi dot org
10-Dec-2004 06:55
10-Dec-2004 06:55
You can also access the node as an array to get attributes:
<?php
$xml = simplexml_load_file('file.xml');
echo 'Attribute: ' . $xml['attribute'];
?>
inge at elektronaut dot no
26-May-2004 05:53
26-May-2004 05:53
here's a simple function to get an attribute by name, based on the example
<?php
function findAttribute($object, $attribute) {
foreach($object->attributes() as $a => $b) {
if ($a == $attribute) {
$return = $b;
}
}
if($return) {
return $return;
}
}
?>
