How To Display XML File In PHP | Free Source Code

How to Display XML File in PHP

June 1, 2020
PHP
display xml file in php

Getting Started

This tutorial will explain how to display XML files.

I’ve used bootstrap to improve the design of the 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 a 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.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Display XML File using PHP</title>
    <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <h1 class="page-header text-center">Display XML File using PHP</h1>
    <div class="row">
        <div class="col-sm-8 col-sm-offset-2">
            <table class="table table-bordered table-striped">
                <thead>
                    <th>UserID</th>
                    <th>Firstname</th>
                    <th>Lastname</th>
                    <th>Address</th>
                </thead>
                <tbody>
                    <?php
                    //load xml file
                    $file = simplexml_load_file('files/members.xml');
 
                    foreach($file->user as $row){
                        ?>
                        <tr>
                            <td><?php echo $row->id; ?></td>
                            <td><?php echo $row->firstname; ?></td>
                            <td><?php echo $row->lastname; ?></td>
                            <td><?php echo $row->address; ?></td>
                        </tr>
                        <?php
                    }
 
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
 
</body>
</html>

That ends this tutorial. Happy Coding!

Download Code
Facebook Comments

Leave a Reply

Your email address will not be published.