PHP - Generate XML File

In this tutorial we will create a Generate XML File using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_xml, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = new mysqli('localhost', 'root', '', 'db_xml');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, 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. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Generate XML File</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button type="button" class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add member</button>
  18. <a href="export.php" class="btn btn-primary pull-right"><span class="glyphicon glyphicon-export"></span> Export as XML</a>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Address</th>
  26. </tr>
  27. </thead>
  28. <tbody style="background-color:#fff;">
  29. <?php
  30. require 'conn.php';
  31. $query = $conn->query("SELECT * FROM `member`") or die($conn->error());
  32. while($fetch = $query->fetch_array()){
  33. ?>
  34. <tr>
  35. <td><?php echo $fetch['firstname']?></td>
  36. <td><?php echo $fetch['lastname']?></td>
  37. <td><?php echo $fetch['address']?></td>
  38. </tr>
  39. <?php
  40. }
  41. ?>
  42. <tbody>
  43. </table>
  44. </div>
  45. <div class="modal fade" id="form_modal" tabindex="-1" role="dialog" aria-hidden="true">
  46. <div class="modal-dialog" role="document">
  47. <form action="save_member.php" method="POST" enctype="multipart/form-data">
  48. <div class="modal-content">
  49. <div class="modal-body">
  50. <div class="col-md-2"></div>
  51. <div class="col-md-8">
  52. <div class="form-group">
  53. <label>Firstname</label>
  54. <input type="text" name="firstname" class="form-control" required="required"/>
  55. </div>
  56. <div class="form-group">
  57. <label>Lastname</label>
  58. <input type="text" name="lastname" class="form-control" required="required"/>
  59. </div>
  60. <div class="form-group">
  61. <label>Address</label>
  62. <input type="text" name="address" class="form-control" required="required"/>
  63. </div>
  64. </div>
  65. </div>
  66. <div style="clear:both;"></div>
  67. <div class="modal-footer">
  68. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  69. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  70. </div>
  71. </div>
  72. </form>
  73. </div>
  74. </div>
  75. </body>
  76. <script src="js/jquery-3.2.1.min.js"></script>
  77. <script src="js/bootstrap.js"></script>
  78. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the data inputs to the database server. To do that just copy and write this block of codes inside the text editor, then save it as save_member.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $firstname = $_POST['firstname'];
  6. $lastname = $_POST['lastname'];
  7. $address = $_POST['address'];
  8.  
  9. $conn->query("INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die($conn->error());
  10.  
  11. header('location: index.php');
  12. }
  13.  
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will get the data from the database server, then will convert and export it as xml file. To do this just copy and write the code inside the text editor, then save it as export.php
  1. <?php
  2. require 'conn.php';
  3. $xml = new DomDocument("1.0", "UTF-8");
  4.  
  5. $person = $xml->createElement("person");
  6. $person = $xml->appendChild($person);
  7.  
  8. $query = $conn->query("SELECT * FROM `member`") or die(mysqli_error());
  9.  
  10. while($fetch = $query->fetch_array()){
  11.  
  12. $member = $xml->createElement("member");
  13. $member = $person->appendChild($member);
  14.  
  15. $firstname = $xml->createElement("firstname", $fetch['firstname']);
  16. $firstname = $member->appendChild($firstname);
  17.  
  18. $lastname = $xml->createElement("lastname", $fetch['lastname']);
  19. $lastname = $member->appendChild($lastname);
  20.  
  21. $address = $xml->createElement("address", $fetch['address']);
  22. $address = $member->appendChild($address);
  23.  
  24. }
  25.  
  26. $xml->FormatOutput = true;
  27. $string_value = $xml->saveXML();
  28. $xml->save("member.xml");
  29.  
  30. ?>
  31. <!DOCTYPE html>
  32. <html lang="en">
  33. <head>
  34. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  35. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  36. </head>
  37. <body>
  38. <nav class="navbar navbar-default">
  39. <div class="container-fluid">
  40. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  41. </div>
  42. </nav>
  43. <div class="col-md-3"></div>
  44. <div class="col-md-6 well">
  45. <h3 class="text-primary">PHP - Generate XML File</h3>
  46. <hr style="border-top:1px dotted #ccc;"/>
  47. <center><h4>XML File has Been Generated</h4></center>
  48. <center><a href="member.xml">Click here to open the file</a></center>
  49. <a href="index.php">Back</a>
  50. </body>
  51.  
  52. </html>
There you have it we successfully created Generate XML File using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Comments

Add new comment