The bogus Behaviour reported in the last two comments was for me with version 5.2.x, after an upgrade to PHP 5.2.5 this was not the case any longer. I could not find any relating bug under bugs.php.net so maybe this was just a fix made.
SimpleXMLElement->getName
(No version information available, might be only in CVS)
SimpleXMLElement->getName — XML 要素の名前を取得する
説明
SimpleXMLElement
string getName
( void
)
XML 要素の名前を取得します。
返り値
getName メソッドは、 SimpleXMLElement オブジェクトが参照している XML タグの名前を string で返します。
例
例1 XML 要素の名前の取得
<?php
$sxe = new SimpleXMLElement($xmlstr);
echo $sxe->getName() . "\n";
foreach ($sxe->children() as $child)
{
echo $child->getName() . "\n";
}
?>
SimpleXMLElement->getName
Anonymous
18-Mar-2008 12:32
18-Mar-2008 12:32
Anonymous
08-Mar-2008 11:11
08-Mar-2008 11:11
In Response to:
Some unexpected behaviour noticed:
<?php
echo $root->parent->child->getName();
?>
"child" is expected, although "parent" is returned
My Suggestion is to use
<?php
echo $root->parent[0]->child[0]->getName();
?>
Instead. I dunno why, but this works.
Anonymous
03-Jan-2008 08:40
03-Jan-2008 08:40
$root = new SimplexmlElement("<root><parent><child/></parent></root>");
foreach($root->parent->children() as $child){
print "</br>".$child->getName();
}
//This will return the child though
m0sh3 at Hotmail dot com
01-Jul-2007 03:25
01-Jul-2007 03:25
Some unexpected behaviour noticed:
<?php
$root = new SimplexmlElement("<root><parent><child/></parent></root>");
echo $root->parent->child->getName();
?>
"child" is expected, although "parent" is returned
