PHP - Simple Load XML Data

In this tutorial we will create a Simple Load XML Data using PHP. This code can display XML file into a readable HTML table. The code use simplexml_load_file to call a block of data within the xml file when the user click the button. This is a user-friendly kind of program feel free to modify it. We will be using XML as a markup language that utilize in php as a HTML data. It is designed to store and transport data that can be manipulated within the local server.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class = "navbar navbar-default">
  9.                 <div class = "container-fluid">
  10.                         <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class = "col-md-3"></div>
  14.         <div class = "col-md-6 well">
  15.                 <h3 class = "text-primary">PHP - Simple Load XML Data</h3>
  16.                 <hr style = "border-top:1px dotted #000;" />
  17.                 <form method="POST">
  18.                         <button name="load" class="btn btn-primary"><span class="glyphicon glyphicon-book"></span> Load XML</button>
  19.                 </form>
  20.                 <br /><br />
  21.                 <?php include 'load_xml.php'?>
  22.         </div>
  23. </body>
  24. </html>

Creating the Main Function

This code contains the main function of the application. This code will load and display a xml data when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. data.xml John Smith Male I
B
Claire Temple I
A
load_xml.php
  1. <?php
  2.         if(ISSET($_POST['load'])){
  3. ?>
  4.  
  5. <table class="table table-bordered" >  
  6.         <thead class="alert-info">
  7.                 <tr>
  8.                         <th>Name</th>
  9.                         <th>Gender</th>
  10.                         <th>Year</th>
  11.                         <th>Section</th>
  12.                 </tr>
  13.         </thead>
  14.        
  15.         <tbody>
  16.        
  17.                 <?php
  18.                         $xml = simplexml_load_file('data.xml');
  19.                         foreach($xml->student as $student){
  20.                                 echo '
  21.                                         <tr>
  22.                                                 <td>'.$student->name.'</td>
  23.                                                 <td>'.$student->gender.'</td>
  24.                                                 <td>'.$student->year.'</td>
  25.                                                 <td>'.$student->section.'</td>
  26.                                         </tr>
  27.                                 ';
  28.                         }
  29.                 ?>
  30.         </tbody>
  31. </table>
  32. <?php
  33.         }else{
  34. ?>
  35. <table class="table table-bordered">   
  36.         <thead class="alert-info">
  37.                 <tr>
  38.                         <th>Name</th>
  39.                         <th>Gender</th>
  40.                         <th>Year</th>
  41.                         <th>Section</th>
  42.                 </tr>
  43.         </thead>
  44.        
  45.         <tbody>
  46.        
  47.         </tbody>
  48. </table>
  49. <?php
  50.         }
  51. ?>
There you have it we successfully created Simple Load XML Data using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment