Skip to main content

PHP - Simple Import XML File


In this tutorial we will create a Simple Import XML File using PHP. This code can import XML file to a data table when user upload the xml file. The code use simplexml_load_file to load a block of data from the uploaded xml file in order to view as a readable table format. This is a user-friendly kind of program feel free to modify it.

We will be using XML as a markup language that utilize in php as a HTML data. It is designed to store and transport data that can be manipulated within the local server.

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.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

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 - Simple Import XML File</h3>
  16. <hr style="border-top:1px dotted #000;" />
  17. <div class="col-md-4">
  18. <form method="POST" action="" enctype="multipart/form-data">
  19. <h5>Upload XML here</h5>
  20. <input type="file" name="file"/>
  21. <br />
  22. <center><button name="import" class="btn btn-primary">Import</button></center>
  23. </form>
  24. </div>
  25. <div class="col-md-8">
  26. <?php include'import.php'?>
  27. </div>
  28. </div>
  29. </body>
  30. </html>

Creating the Main Function

This code contains the main function of the application. This code will upload and display a xml data when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as import.php.

  1. <?php
  2. if(ISSET($_POST['import'])){
  3. $file_name=$_FILES['file']['name'];
  4. $exp=explode('.', $file_name);
  5. $name=end($exp);
  6.  
  7. if($name=="xml"){
  8. ?>
  9. <table class="table table-bordered" >
  10. <thead class="alert-info">
  11. <tr>
  12. <th>Firstname</th>
  13. <th>Lastname</th>
  14. <th>Address</th>
  15. </tr>
  16. </thead>
  17.  
  18. <tbody>
  19.  
  20. <?php
  21. $xml = simplexml_load_file(''.$file_name);
  22. foreach($xml->member as $member){
  23. echo '
  24. <tr>
  25. <td>'.$member->firstname.'</td>
  26. <td>'.$member->lastname.'</td>
  27. <td>'.$member->address.'</td>
  28. </tr>
  29. ';
  30. }
  31. ?>
  32. </tbody>
  33. </table>
  34. <?php
  35. }else{
  36. echo"<script>alert('Please upload xml file only')</script>";
  37. }
  38. }else{
  39. ?>
  40. <table class="table table-bordered" >
  41. <thead class="alert-info">
  42. <tr>
  43. <th>Firstname</th>
  44. <th>Lastname</th>
  45. <th>Address</th>
  46. </tr>
  47. </thead>
  48.  
  49. <tbody>
  50. <td></td>
  51. <td></td>
  52. <td></td>
  53. </tbody>
  54. </table>
  55. <?php
  56. }
  57. ?>

There you have it we successfully created Simple Import 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!

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.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.