Skip to main content

How to Display XML File using PHP


Getting Started

In the previous tutorial, we have discussed on How to create XML file from MySQL Database using PHP. As a continuation, this tutorial will discuss on how to display XML files. I've used bootstrap to improve the design of presentation of this tutorial. This bootstrap is included in the downloadable of this tutorial but, if you want, you may download bootstrap using this link.

Creating our Script

I have included a sample .xml file in the downloadable of this tutorial which I'm going to use to display in the tables of this tutorial. Create a new file named index.php and paste the codes below.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Display XML File using PHP</title>
  6. <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">Display XML File using PHP</h1>
  11. <div class="row">
  12. <div class="col-sm-8 col-sm-offset-2">
  13. <table class="table table-bordered table-striped">
  14. <thead>
  15. <th>UserID</th>
  16. <th>Firstname</th>
  17. <th>Lastname</th>
  18. <th>Address</th>
  19. </thead>
  20. <tbody>
  21. <?php
  22. //load xml file
  23. $file = simplexml_load_file('files/members.xml');
  25. foreach($file->user as $row){
  26. ?>
  27. <tr>
  28. <td><?php echo $row->id; ?></td>
  29. <td><?php echo $row->firstname; ?></td>
  30. <td><?php echo $row->lastname; ?></td>
  31. <td><?php echo $row->address; ?></td>
  32. </tr>
  33. <?php
  34. }
  36. ?>
  37. </tbody>
  38. </table>
  39. </div>
  40. </div>
  41. </div>
  43. </body>
  44. </html>
That ends this tutorial. Happy Coding :)


Submitted byoleteacheron Wed, 03/14/2018 - 02:11

Hi Neovic. I really enjoy your tutorials and they have helped me learn a lot, look forward to seeing more. If I may request a tutorial based on "How to Display XML File using PHP": How to write new entries and update existing entries to the members.xml file with PHP only (no database). Many times using mySQL database is too heavy for simple needs. Thanks again for the tutorials! Susan

Add new comment