PHP - Simple Load XML Data

Submitted by: 
Language: 
Visitors have accessed this post 239 times.

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 = "<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class = "col-md-3"></div>
  15. <div class = "col-md-6 well">
  16. <h3 class = "text-primary">PHP - Simple Load XML Data</h3>
  17. <hr style = "border-top:1px dotted #000;" />
  18. <form method="POST">
  19. <button name="load" class="btn btn-primary"><span class="glyphicon glyphicon-book"></span> Load XML</button>
  20. </form>
  21. <br /><br />
  22. <?php include 'load_xml.php'?>
  23. </div>
  24. </body>
  25. </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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.