PHP - Funzione XMLReader :: moveToElement ()
Definizione e utilizzo
XML è un linguaggio di markup per condividere i dati sul Web, XML è sia leggibile dall'uomo che dalla macchina. L'estensione XMLReader viene utilizzata per leggere / recuperare il contenuto di un documento XML, ovvero utilizzando i metodi della classe XMLReader è possibile leggere ogni nodo di un documento XML.
Il XMLReader::moveToElement() La funzione della classe XMLReader sposta il cursore sull'elemento padre dell'attributo corrente nel documento XML.
Sintassi
XMLReader::moveToElement(); 
    Parametri
Questa funzione non accetta alcun parametro.
Valori restituiti
Questa funzione restituisce un valore booleano che è TRUE in caso di successo e FALSE in caso di fallimento.
Versione PHP
Questa funzione è stata introdotta per la prima volta nella versione 5 di PHP e funziona in tutte le versioni successive.
Esempio
L'esempio seguente mostra l'utilizzo di XMLReader::moveToElement() funzione -
data.xml
<Employee>
   <Name id = "name">Krishna</Name>
   <Age id = "age">22</Age>
   <City id = "city">Hyderabad</City>   
   <Phone id = "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
   while($reader->read()){
      if ($reader->nodeType == XMLREADER::ELEMENT) { 
         $res = $reader->moveToAttribute('id'); 
         $reader->moveToElement(); 
         print($reader->name."\n");
      }
   }
   //Closing the reader
   $reader->close();
?> 
    Questo produrrà il seguente risultato:
Employee
Name
Age
City
Phone 
    Esempio
Di seguito è riportato un altro esempio di questa funzione:
test.xml
<data> 
   <name att = "test_attribute">Raju</name> 
   <age>32</age> 
   <phone>9848022338</phone> 
	<city>Hyderabad</city>
</data> 
    sample.php
<?php
   //Creating an XMLReader
   $reader = new XMLReader();
   //Opening a reader
   $reader->open("trail.xml");
   //Reading the contents
   $reader->next();
   $reader->read();
   $reader->next();
   $reader->moveToAttribute("att");
   $reader->moveToElement(); 
   print($reader->name."\n");
   //Closing the reader
   $reader->close();
?> 
    Questo produrrà il seguente risultato:
near 
    Esempio
<?php
   //Creating an XMLReader
   $reader = new XMLReader();
   $data = "<data> 
      <name att = 'test_attribute'>Raju</name> 
      <age>32</age> 
      <phone>9848022338</phone> 
      <city>Hyderabad</city>
   </data> ";
   //Opening a reader
   $reader->xml($data);
   //Reading the contents
   $reader->next();
   $reader->read();
   $reader->next();
   $reader->moveToAttribute("att");
   $reader->moveToElement(); 
   print($reader->name."\n");
   //Closing the reader
   $reader->close();
?> 
    Questo produrrà il seguente risultato:
test_attribute 
                        