Skip to main content

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

  1. <?xml version="1.0"?>
  2.  
  3. <students>
  4. <student>
  5. <name>John Smith</name>
  6. <gender>Male</gender>
  7. <year>I</year>
  8. <section>B</section>
  9. </student>
  10. <student>
  11. <name>Claire</name>
  12. <gender>Temple</gender>
  13. <year>I</year>
  14. <section>A</section>
  15. </student>
  16. </students>

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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.