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() — Récupère le nom d'un élément XML
Description
SimpleXMLElement
string getName
( void
)
Récupère le nom d'un élément XML.
Valeurs de retour
La méthode getName retourne un nom sous la forme de chaîne de caractères d'une balise XML référencé par l'objet SimpleXMLElement.
Exemples
Exemple #1 Récupère les noms des éléments 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 03:32
18-Mar-2008 03:32
Anonymous
09-Mar-2008 03:11
09-Mar-2008 03: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
04-Jan-2008 12:40
04-Jan-2008 12: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 06:25
02-Jul-2007 06: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
