PHP Simple XML GET

PHP - Simple XML GET


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP Simpe XML GET.

The simple xml GET is used for getting the node values from an xml file.

Example

Following example shows how to get data from xml.

Note.xml

Note.xml is an xml file, it can be accessed by php file -

<SUBJECT>
   <COURSE>HTML</COURSE>
   <COUNTRY>Nigeria</COUNTRY>
   <COMPANY>Web Design Tutorialz</COMPANY>
   <PRICE>$10</PRICE>
</SUBJECT>


Index.html

The index page has the right to get access to the xml data using simplexml_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>

Output

When the above code is executed, it will produce the following result -



Get Node Values

The following code below has information about how to obtain node values from an xml file. The xml should be as follows -

<?xml version = "1.0" encoding = "utf-8"?>
<webdesigntutorialz>
   
   <course category = "HTML">
      <title lang = "en">HTML</title>
      <tutor>Web Design Tutorialz Team</tutor>
      <duration>3</duration>
      <price>$30</price>
   </course>
   
   <course category = "JavaScript">
      <title lang = "en">JavaScript</title>.
      <tutor>Web Design Tutorialz Team</tutor>
      <duration>3</duration>
      <price>$50</price>
   </course>
   
   <course category = "WorldPress">
      <title lang = "en">WorldPress</title>
      <tutor>Web Design Tutorialz Team</tutor>
      <duration>5</duration>
      <price>$50</price>
   </course>
   
   <course category = "AJAX">
      <title lang = "en">AJAX</title>
      <tutor>Web Design Tutorialz Team</tutor>
      <duration>10</duration>
      <price>$60</price>
   </course>

</webdesigntutorialz>

php file

PHP code should be as follows -

<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>

Output

When the above code is executed, it will produce the following result -



Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about PHP - SAX Parser.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.