Fonction getName() PHP

Définition et utilisation

La fonction getName() récupère le nom de l'élément XML à partir de l'objet SimpleXMLElement.

Si elle réussit, la fonction retourne le nom actuel de l'élément XML. Si elle échoue, elle retourne false.

Syntaxe

class SimpleXMLElement
{
string getName()
}

Exemple

Fichier XML :

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Rappel</heading>
<b:body>Ne pas oublier la réunion !</b:body>
</note>

Code PHP :

<?php
if (file_exists('test.xml'))
  {
  $xml = simplexml_load_file('test.xml');
  }
echo $xml->getName();
foreach($xml->children() as $child)
  {
  echo $child->getName();
  }
?>

La sortie est similaire à :

note
to
from
heading
body