How to Display XML File using PHP

Submitted by: 
Visitors have accessed this post 366 times.

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 :)


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!


Thank you for the appreciation. As per request, here's the link to the tutorial I've created.

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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.