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()
(PHP 5 >= 5.1.3)
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 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
