Display XML File Using HTML/PHP PHP Script


Display XML File Using HTML/PHP PHP Script

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"?>

<employees>
<employee>
<name>Tony Stark</name>
<age>30</age>
<gender>Male</gender>
<address>New York</address>
</employee>

<employee>
<name>Steeve Roger</name>
<age>25</age>
<gender>Male</gender>
<address>New York</address>
</employee>

<employee>
<name>Natasha</name>
<age>24</age>
<gender>Female</gender>
<address>Britain</address>
</employee>

<employee>
<name>Albert Einstein</name>
<age>50</age>
<gender>Male</gender>
<address>Germany</address>
</employee>

<employee>
<name>Nikola Tesla</name>
<age>65</age>
<gender>Male</gender>
<address>Crotia</address>
</employee>

<employee>
<name>Thomas Edison</name>
<age>60</age>
<gender>Male</gender>
<address>Ohio</address>
</employee>

<employee>
<name>Henry Ford</name>
<age>50</age>
<gender>Male</gender>
<address>Greenfield</address>
</employee>

</employees>

 

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 = "<a href="https://sourcecodester.com">Sourcecodester</a>
" rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
</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
$xml = simplexml_load_file('phptut.xml');
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>

 

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!!!

 

 

Read Me

 



Find More Display XML File Using HTML/PHP PHP Script

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Website

Wordpress  Create Free Website (Web Builder)
Uploadfile  Upload File Website (Unlimited)
Mobilelist  Mobile Price Website (Mobilelop)
Blog  Create Blog Website (Wapfo)
Wapbuilder  Create Free Wapsite (Wapbuilder)