PHP - Fonction XMLReader :: moveToNextAttribute ()

Définition et utilisation

XML est un langage de balisage permettant de partager les données sur le Web, XML est à la fois lisible par l'homme et par machine. L'extension XMLReader est utilisée pour lire / récupérer le contenu d'un document XML, c'est-à-dire en utilisant les méthodes de la classe XMLReader, vous pouvez lire chaque nœud d'un document XML.

le XMLReader::moveToNextAttribute() La fonction de la classe XMLReader déplace le curseur sur l'attribut suivant dans le document XML.

Syntaxe

XMLReader::moveToAttribute();

Paramètres

Cette fonction n'accepte aucun paramètre.

Valeurs de retour

Cette fonction renvoie une valeur booléenne qui est TRUE en cas de succès et FALSE en cas d'échec.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation du XMLReader::moveToNextAttribute() fonction -

data.xml

<Employee>
   <Name id1 = "attr_name">Krishna</Name>
   <Age id2 = "attr_age">22</Age>
   <City id3 = "attr_city">Hyderabad</City>   
   <Phone id4 = "attr_phone">980000000</Phone>   
</Employee>

sample.php

<?php
   //Creating an XMLReader
   $reader = new XMLReader();

   //Opening a reader
   $reader->open("trail.xml");

   //Reading the contents of the XML file
   $reader->read();
   $reader->read();
   $reader->read();

   if ($reader->nodeType == XMLREADER::ELEMENT) { 
      $reader->moveToFirstAttribute(); 
      print($reader->name."\n");
      
      $reader->moveToNextAttribute(); 
      print($reader->name."\n");
   }
   
   //Closing the reader
   $reader->close();
?>

Cela produira le résultat suivant -

name_attr1
name_attr2

Exemple

Voici un autre exemple de cette fonction -

<?php
   //Creating an XMLReader
   $reader = new XMLReader();

   $data = "<Employee>
      <Name name_attr1 = 'n_val1' name_attr2 = 'n_val2'>Krishna</Name>
      <Age>22</Age>
      <City>Hyderabad</City>   
      <Phone>980000000</Phone>   
   </Employee>";

   //Opening a reader
   $reader->xml($data);

   //Reading the contents of the XML file
   $reader->read();
   $reader->read();
   $reader->read();

   $reader->moveToFirstAttribute(); 
   print($reader->name."\n");
      
   $reader->moveToNextAttribute(); 
   print($reader->name."\n");

   //Closing the reader
   $reader->close();
?>

Cela produira le résultat suivant -

name_attr1
name_attr2