JavaScript - Simple Populate Table With XML Data

In this tutorial we will create a Simple Populate Table With XML Data using JavaScript. This code will automatically import the xml data in the table when user click the button. The code use onclick() function to initiate a certain method that import the xml data using $.parseXML by providing the xml string format as an argument in order to display it in the html table. Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  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. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">JavaScript - Simple Populate Table With XML Data</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <table class="table table-bordered">
  18. <thead class="alert-info">
  19. <tr>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. </tr>
  23. </thead>
  24. <tbody id="result"></tbody>
  25. </table>
  26. </div>
  27. <div class="col-md-2">
  28. <br/><br/>
  29. <center><button type="button" class="btn btn-success" id="generate">Import XML</button></center>
  30. </div>
  31. </div>
  32. </body>
  33. <script src="js/jquery-3.2.1.min.js"></script>
  34. <script src="js/script.js"></script>
  35. </html>

Creating the Script

This code contains the script of the application. This code will import a xml data when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2. $('#generate').on('click', function(){
  3. var data = $.parseXML(xml);
  4.  
  5. var $xml = $(data);
  6.  
  7. var $user = $xml.find("user");
  8.  
  9. var content = "";
  10. $user.each(function(){
  11.  
  12. var firstname = $(this).find('firstname').text();
  13. var lastname = $(this).find('lastname').text();
  14.  
  15. content += "<tr>"
  16. + "<td>"+firstname+"</td>"
  17. + "<td>"+lastname+"</td>"
  18. + "</tr>";
  19.  
  20. $("#result" ).html(content);
  21.  
  22. });
  23. });
  24.  
  25. var xml =
  26. "<?xml version='1.0' ?>"
  27. + "<doc>"
  28. +" <user>"
  29. +" <firstname>Tony</firstname>"
  30. +" <lastname>Stark</lastname>"
  31. +" </user>"
  32. +" <user>"
  33. +" <firstname>Claire</firstname>"
  34. +" <lastname>Temple</lastname>"
  35. +" </user>"
  36. +" <user>"
  37. +" <firstname>John</firstname>"
  38. +" <lastname>Smith</lastname>"
  39. +" </user>"
  40. +"</doc>";
  41. });
There you have it we successfully created a Simple Populate Table With XML Data using JavaScript. 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