How to Create XML file from MySQL Database using PHP/MySQLi

Getting Started

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 Database

Next, we create our MySQL database where we fetch data into XML file. I've included a .sql file in the downloadable of this tutorial which is a mysql database file. All you have to do is import the said file. If you have no idea on how to do this, please refer to my tutorial, How import .sql file to restore MySQL database. You should be able to create a database with tables named mydatabase.

Creating our Link to Create XML

Next, we create the link to create our XML by creating a new file and name it as index.php. Also, I've included showing the data in our table in this file.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.         <meta charset="utf-8">
  5.         <title>Create an XML file from MySQL Database using PHP/MySQLi</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">Create an XML file from MySQL Database</h1>
  11.         <div class="row">
  12.                 <div class="col-sm-8 col-sm-offset-2">
  13.                         <div class="row">
  14.                                 <div class="col-sm-2">
  15.                                         <a href="create_xml.php" class="btn btn-primary btn-sm">Create XML</a>
  16.                                 </div>
  17.                                 <div class="col-sm-10">
  18.                                         <?php
  19.                                                 session_start();
  20.  
  21.                                                 if(isset($_SESSION['message'])){
  22.                                                         echo $_SESSION['message'];
  23.  
  24.                                                         unset($_SESSION['message']);
  25.                                                 }
  26.                                         ?>
  27.                                 </div>
  28.                         </div>
  29.                         <table class="table table-bordered table-striped" style="margin-top:20px;">
  30.                                 <thead>
  31.                                         <th>UserID</th>
  32.                                         <th>Firstname</th>
  33.                                         <th>Lastname</th>
  34.                                         <th>Address</th>
  35.                                 </thead>
  36.                                 <tbody>
  37.                                         <?php
  38.                                         //connection
  39.                                         $conn = new mysqli('localhost', 'root', '', 'mydatabase');
  40.  
  41.                                         $sql = "SELECT * FROM members";
  42.                                         $query = $conn->query($sql);
  43.  
  44.                                         while($row = $query->fetch_array()){
  45.                                                 ?>
  46.                                                 <tr>
  47.                                                         <td><?php echo $row['id']; ?></td>
  48.                                                         <td><?php echo $row['firstname']; ?></td>
  49.                                                         <td><?php echo $row['lastname']; ?></td>
  50.                                                         <td><?php echo $row['address']; ?></td>
  51.                                                 </tr>
  52.                                                 <?php
  53.                                         }
  54.  
  55.                                         ?>
  56.                                 </tbody>
  57.                         </table>
  58.                 </div>
  59.         </div>
  60. </div>
  61. </body>
  62. </html>

Creating our Create XML Script

Lastly, we create our script that creates XML file from our MySQL Database. Create a new file named create_xml.php and paste the below codes.
  1. <?php
  2.         session_start();
  3.         //connection
  4.         $conn = new mysqli('localhost', 'root', '', 'mydatabase');
  5.  
  6.         //fetching data from mysql into array
  7.         $data = array();
  8.         $sql = "SELECT * FROM members";
  9.         $query = $conn->query($sql);
  10.  
  11.         while($row = $query->fetch_assoc()){
  12.                 $data[] = $row;
  13.         }
  14.  
  15.         //converting our array into xml file
  16.     //create the xml document
  17.     $xml = new DOMDocument();
  18.     $root = $xml->createElement('users');
  19.  
  20.     foreach($data as $user){
  21.             $userRow = $root->appendChild($xml->createElement('user'));
  22.  
  23.             //populate user info
  24.             foreach($user as $key => $val){
  25.                 $userRow->appendChild($xml->createElement($key, $val));
  26.             }
  27.        
  28.     }
  29.  
  30.     $xml->appendChild($root);
  31.     header("Content-Type: text/plain");
  32.     //make the output pretty
  33.     $xml->formatOutput = true;
  34.     //save xml file
  35.     $xml->save('files/members.xml');
  36.  
  37.     $_SESSION['message'] = 'XML file created. Check your files folder';
  38.     header('location: index.php');
  39.  
  40. ?>
P.S. Don't forget to create a folder named files. That is where the created XML files are saved. That ends this tutorial. Happy Coding :)

Add new comment