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 — Gets the name of the XML element
Description
SimpleXMLElement
string getName
( void
)
Gets the name of the XML element.
Return Values
The getName method returns as a string the name of the XML tag referenced by the SimpleXMLElement object.
Examples
Example #1 Get XML element names
<?php
$sxe = new SimpleXMLElement($xmlstr);
echo $sxe->getName() . "\n";
foreach ($sxe->children() as $child)
{
echo $child->getName() . "\n";
}
?>
SimpleXMLElement->getName
Anonymous
18-Mar-2008 08:32
18-Mar-2008 08:32
Anonymous
08-Mar-2008 08:11
08-Mar-2008 08: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 05:40
03-Jan-2008 05: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
02-Jul-2007 12:25
02-Jul-2007 12: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
