How to Display XML Data using jQuery

How to Display XML Data using jQuery

Introduction

In this tutorial we will create a How to Display XML Data using jQuery. This tutorial purpose is to teach how to you will display the xml data into the html page. This will tackle the process for display all the xml data. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is very easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use if you want to secure some important data in your site. I will try my best to give you the easiest way of creating this program Displaying of xml. So let's do the coding.

Before we get started:

First you have to download the jQuery plugin.

Here is the link for the jQuery that i used in this tutorial https://jquery.com/.

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

Creating The Interface

This is where we will create a simple interface for our application. This code will display only an empty table and a simple button. To create this simply copy and write it into your text editor, then save it 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">How to Display XML Data using jQuery</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <button type="button" class="btn btn-success" id="display">Display Data</button>
  17. <br /><br />
  18. <table class="table table-bordered">
  19. <tr>
  20. <th>Firstname</th>
  21. <th>Lastname</th>
  22. <th>Age</th>
  23. </tr>
  24. </thead>
  25. <tbody id="result" style="background-color:#fff;"></tbody>
  26. </table>
  27. </div>
  28. </body>
  29. <script src="js/jquery-3.2.1.min.js"></script>
  30. <script src="script.js"></script>
  31. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will automatically translate and display the xml data into the html table. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. $(document).ready(function(){
  2.  
  3. let xml =
  4. "<?xml version='1.0' ?>"
  5. + "<doc>"
  6. +" <member>"
  7. +" <firstname>John</firstname>"
  8. +" <lastname>Smith</lastname>"
  9. +" <age>21</age>"
  10. +" </member>"
  11. +" <member>"
  12. +" <firstname>Steve</firstname>"
  13. +" <lastname>Roger</lastname>"
  14. +" <age>25</age>"
  15. +" </member>"
  16. +" <member>"
  17. +" <firstname>Natasha</firstname>"
  18. +" <lastname>Romanof</lastname>"
  19. +" <age>19</age>"
  20. +" </member>"
  21. +" <member>"
  22. +" <firstname>Yor</firstname>"
  23. +" <lastname>Forger</lastname>"
  24. +" <age>26</age>"
  25. +" </member>"
  26. +" <member>"
  27. +" <firstname>Tony</firstname>"
  28. +" <lastname>Stark</lastname>"
  29. +" <age>35</age>"
  30. +" </member>"
  31. +" </doc>";
  32.  
  33.  
  34. $('#display').on('click', function(){
  35. let data = $.parseXML(xml);
  36.  
  37. let $xml = $(data);
  38.  
  39. let $member = $xml.find("member");
  40.  
  41. let content = "";
  42. $member.each(function(){
  43.  
  44. let firstname = $(this).find('firstname').text();
  45. let lastname = $(this).find('lastname').text();
  46. let age = $(this).find('age').text();
  47.  
  48. content += "<tr>"
  49. + "<td>"+firstname+"</td>"
  50. + "<td>"+lastname+"</td>"
  51. + "<td>"+age+"</td>"
  52. + "</tr>";
  53.  
  54. $("#result" ).html(content);
  55.  
  56. });
  57. });
  58.  
  59.  
  60. });

In the given code we first call the basic jQuery ready event to signal that the DOM of the page is now ready to be use. After that we create new variable that will be the xml data and called it xml.We then bind the button trigger event using the on() function. We first then set a variable called data that will parse the our xml data using the $.parseXML() function and set it to a new variable called $xml. Next we will need to find a specific data using the find() function and set it as $member variable.

In order to display the xml data into the table we need to loop the data by calling the parent data first and finding the children data, then append all of it using the html() function

Output:



The How to Display XML Data using jQuery source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Display XML Data using jQuery. 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment