PHP - GET XML simple

XML Get a utilisé pour obtenir les valeurs de nœud à partir d'un fichier xml. L'exemple suivant montre comment obtenir les données à partir de xml.

Note.xml

Note.xml est un fichier xml, il est accessible par fichier php.

<SUBJECT>
   <COURSE>Android</COURSE>
   <COUNTRY>India</COUNTRY>
   <COMPANY>TutorialsPoint</COMPANY>
   <PRICE>$10</PRICE>
</SUBJECT>

Index.htm

La page d'index a le droit d'accéder aux données xml en utilisant implexml_load_file ().

<?php
   $xml = simplexml_load_file("note.xml") or die("Error: Object Creation failure");
?>

<html>
   <head>
      
      <body>
         
         <?php
            echo $xml->COURSE . "<br>";
            echo $xml->COUNTRY . "<br>";
            echo $xml->COMPANY . "<br>";
            echo $xml->PRICE;
         ?>
         
      </body>
      
   </head>
</html>

Il produira le résultat suivant -

Obtenir les valeurs de nœud

Le code ci-dessous contient des informations sur la façon d'obtenir des valeurs de nœud à partir d'un fichier xml et XML devrait être comme suit -

<?xml version = "1.0" encoding = "utf-8"?>
<tutorialspoint>
   
   <course category = "JAVA">
      <title lang = "en">Java</title>
      <tutor>Gopal</tutor>
      <duration></duration>
      <price>$30</price>
   </course>
   
   <course category = "HADOOP">
      <title lang = "en">Hadoop</title>.
      <tutor>Satish</tutor>
      <duration>3>/duration>
      <price>$50</price>
   </course>
   
   <course category = "HTML">
      <title lang = "en">html</title>
      <tutor>raju</tutor>
      <duration>5</duration>
      <price>$50</price>
   </course>
   
   <course category = "WEB">
      <title lang = "en">Web Technologies</title>
      <tutor>Javed</tutor>
      <duration>10</duration>
      <price>$60</price>
   </course>

</tutorialspoint>

Le code PHP doit être le suivant

<html>
   <body>
   
      <?php
         $xml = simplexml_load_file("books.xml") or die("Error: Cannot create object");
         
         foreach($xml->children() as $books) { 
            echo $books->title . "<br> "; 
            echo $books->tutor . "<br> "; 
            echo $books->duration . "<br> ";
            echo $books->price . "<hr>"; 
         }
      ?>
      
   </body>
</html>

Il produira le résultat suivant -