PHP - How To Display XML File Using HTML/PHP

Language
In this tutorial we will try to read a Simple XML File using PHP script. XML used to create common information formats and data encryption. Some of XML's advantages are its self-documenting which allows for internationalization in human language that support parsing requirements. By using XML it improved the exponential and simplified capacity of PHP 5. So Let's see how it do. Before we started: First you have to download & install WAMPserver or any local server that run PHP scripts. Here's the link for WAMP server http://www.wampserver.com/en/. Creating The XML File This is where the XML File store all the data that will be later be display via PHP script. Just copy and paste the code below then save it as "phptut.xml" Tony Stark 30 Male
New York
Steeve Roger 25 Male
New York
Natasha 24 Female
Britain
Albert Einstein 50 Male
Germany
Nikola Tesla 65 Male
Crotia
Thomas Edison 60 Male
Ohio
Henry Ford 50 Male
Greenfield
The PHP Script This is where the main function happened. The PHP script decrypt the XML file into a readable content to be able to view in the web browser. To do that just simply copy and paste the code below 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.                 <link rel = "stylesheet" type = "text/css" href = "css/jquery.dataTables.css"/>
  7.         </head>
  8. <body>
  9.         <nav class = "navbar navbar-default">
  10.                 <div class = "container-fluid">
  11.                         <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class = "col-md-3"></div>
  15.         <div class = "col-md-6 well">
  16.                 <h3 class = "text-primary">PHP - How To Display XML File Using HTML/PHP</h3>
  17.                 <hr style = "border-top:1px dotted #000;" />
  18.                 <table id = "table" class = "table table-bordered" >   
  19.                         <thead>
  20.                                 <tr>
  21.                                         <th>Name</th>
  22.                                         <th>Age</th>
  23.                                         <th>Gender</th>
  24.                                         <th>Address</th>
  25.                                 </tr>
  26.                         </thead>
  27.                        
  28.                         <tbody>
  29.                        
  30.                                 <?php
  31.                                         $xml = simplexml_load_file('phptut.xml');
  32.                                         foreach($xml->employee as $employee){
  33.                                                 echo '
  34.                                                         <tr>
  35.                                                                 <td>'.$employee->name.'</td>
  36.                                                                 <td>'.$employee->age.'</td>
  37.                                                                 <td>'.$employee->gender.'</td>
  38.                                                                 <td>'.$employee->address.'</td>
  39.                                                         </tr>
  40.                                                 ';
  41.                                         }
  42.                                 ?>
  43.                         </tbody>
  44.                 </table>
  45.         </div>
  46. </body>
  47.  
  48. <script src = "js/jquery-3.2.1.js"></script>
  49. <script src = "js/jquery.dataTables.js"></script>
  50.  
  51. <script type = "text/javascript">
  52.         $(document).ready(function(){
  53.                   $('#table').DataTable();
  54.         });
  55. </script>
  56. </html>
There you have it we've able to display a XML file into the web browser using PHP. I hope that this simple tutorial help you understand how a XML file worked with PHP script. For more updates and tutorial 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.

Tags

Comments

I have downloaded this and like it. How can I modify it so the table can be wider without widening my browser window? I am not a programmer, but dabble and can't quite find it.

Add new comment