PHP - How To Display XML File Using HTML/PHP
Submitted by razormist on Wednesday, June 21, 2017 - 16:45.
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"
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!!!
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"
- <!DOCTYPE html>
- <html lang = "en">
- <head>
- <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1" />
- <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css"/>
- <link rel = "stylesheet" type = "text/css" href = "css/jquery.dataTables.css"/>
- </head>
- <body>
- <nav class = "navbar navbar-default">
- <div class = "container-fluid">
- <a class = "navbar-brand" href = "https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class = "col-md-3"></div>
- <div class = "col-md-6 well">
- <h3 class = "text-primary">PHP - How To Display XML File Using HTML/PHP</h3>
- <hr style = "border-top:1px dotted #000;" />
- <table id = "table" class = "table table-bordered" >
- <thead>
- <tr>
- <th>Name</th>
- <th>Age</th>
- <th>Gender</th>
- <th>Address</th>
- </tr>
- </thead>
- <tbody>
- <?php
- foreach($xml->employee as $employee){
- echo '
- <tr>
- <td>'.$employee->name.'</td>
- <td>'.$employee->age.'</td>
- <td>'.$employee->gender.'</td>
- <td>'.$employee->address.'</td>
- </tr>
- ';
- }
- ?>
- </tbody>
- </table>
- </div>
- </body>
- <script src = "js/jquery-3.2.1.js"></script>
- <script src = "js/jquery.dataTables.js"></script>
- <script type = "text/javascript">
- $(document).ready(function(){
- $('#table').DataTable();
- });
- </script>
- </html>
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.
Comments
Add new comment
- Add new comment
- 1153 views