Thursday, October 15, 2020

PHP Simple XML GET



Hello folks! welcome to another section of my tutorial on PHP. In this section of my PHP tutorial, we will be discussing 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 rights to get access to the xml data using the 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 below code 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 rounding up for this tutorial post. In my next tutorial guide, we are going to be discussing about PHP - SAX Parser.

Feel free to ask your questions where necessary and i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.