• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - How To Display XML File Using HTML/PHP

thenuk3r

Landing Page Optimizer
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
94
Likes
135
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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"

xml version="1.0"?

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. <tbody>

  28. <?php
  29. $xml

    =

    simplexml_load_file

    (

    'phptut.xml'

    )

    ;
  30. foreach

    (

    $xml

    ->

    employee

    as

    $employee

    )

    {
  31. echo

    '
  32. <tr>
  33. <td>'

    .

    $employee

    ->

    name

    .

    '</td>
  34. <td>'

    .

    $employee

    ->

    age

    .

    '</td>
  35. <td>'

    .

    $employee

    ->

    gender

    .

    '</td>
  36. <td>'

    .

    $employee

    ->

    address

    .

    '</td>
  37. </tr>
  38. '

    ;
  39. }
  40. ?>
  41. </tbody>
  42. </table>
  43. </div>
  44. </body>

  45. <script src = "js/jquery-3.2.1.js"></script>
  46. <script src = "js/jquery.dataTables.js"></script>

  47. <script type = "text/javascript">
  48. $(document).ready(function(){
  49. $('#table').DataTable();
  50. });
  51. </script>
  52. </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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top