PHP - Simple Import XML File

Submitted by: 
Language: 
Visitors have accessed this post 106 times.

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 = "<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Simple Import XML File</h3>
  17. <hr style="border-top:1px dotted #000;" />
  18. <div class="col-md-4">
  19. <form method="POST" action="" enctype="multipart/form-data">
  20. <h5>Upload XML here</h5>
  21. <input type="file" name="file"/>
  22. <br />
  23. <center><button name="import" class="btn btn-primary">Import</button></center>
  24. </form>
  25. </div>
  26. <div class="col-md-8">
  27. <?php include'import.php'?>
  28. </div>
  29. </div>
  30. </body>
  31. </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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.