JavaScript - Read And Display XML

In this tutorial we will create a Read And Display XML. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, 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 - Read And Display XML Using jQuery</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <button type="button" class="btn btn-success" id="generate">Generate Data</button>
  17.                 <br /><br />
  18.                 <table class="table table-bordered">
  19.                         <thead class="alert-info">
  20.                                 <tr>
  21.                                         <th>Firstname</th>
  22.                                         <th>Lastname</th>
  23.                                         <th>Address</th>
  24.                                 </tr>
  25.                         </thead>
  26.                         <tbody id="result" style="background-color:#fff;"></tbody>
  27.                 </table>
  28.         </div>
  29. </body>
  30. <script src="js/jquery-3.2.1.min.js"></script>
  31. <script src="js/script.js"></script>
  32. </html>

Creating the Script

This code contains the script of the application. This code will render and display a xml data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2.        
  3.         var xml =
  4.         "<?xml version='1.0' ?>"
  5.         + "<doc>"
  6.         +"              <member>"
  7.         +"                      <firstname>Naruto</firstname>"
  8.         +"                      <lastname>Uzumaki</lastname>"
  9.         +"                      <address>Konoha</address>"
  10.         +"              </member>"
  11.         +"              <member>"
  12.         +"                      <firstname>Sasuke</firstname>"
  13.         +"                      <lastname>Uchiha</lastname>"
  14.         +"                      <address>Konoha</address>"
  15.         +"              </member>"
  16.         +"              <member>"
  17.         +"                      <firstname>Sakura</firstname>"
  18.         +"                      <lastname>Haruna</lastname>"
  19.         +"                      <address>Konoha</address>"
  20.         +"              </member>"
  21.         +"              <member>"
  22.         +"                      <firstname>Yo</firstname>"
  23.         +"                      <lastname>Asakura</lastname>"
  24.         +"                      <address>Tokyo</address>"
  25.         +"              </member>"
  26.         +"      </doc>";
  27.        
  28.        
  29.         $('#generate').on('click', function(){
  30.                 var data = $.parseXML(xml);
  31.        
  32.                 var $xml = $(data);
  33.        
  34.                 var $member = $xml.find("member");
  35.        
  36.                 var content = "";
  37.                 $member.each(function(){
  38.                
  39.                         var firstname = $(this).find('firstname').text();
  40.                         var lastname = $(this).find('lastname').text();
  41.                         var address = $(this).find('address').text();
  42.                        
  43.                         content += "<tr>"
  44.                                 + "<td>"+firstname+"</td>"
  45.                                 + "<td>"+lastname+"</td>"
  46.                                 + "<td>"+address+"</td>"
  47.                                 + "</tr>";
  48.                        
  49.                         $("#result" ).html(content);
  50.                
  51.                 });
  52.         });
  53.        
  54.        
  55. });
There you have it we successfully created a Read And Display XML. 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