JavaScript - Simple Populate Table With XML Data
Submitted by razormist on Friday, July 26, 2019 - 21:32.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-8">
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- </tr>
- </thead>
- </table>
- </div>
- <div class="col-md-2">
- </div>
- </div>
- </body>
- </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.- $(document).ready(function(){
- $('#generate').on('click', function(){
- var data = $.parseXML(xml);
- var $xml = $(data);
- var $user = $xml.find("user");
- var content = "";
- $user.each(function(){
- var firstname = $(this).find('firstname').text();
- var lastname = $(this).find('lastname').text();
- content += "<tr>"
- + "<td>"+firstname+"</td>"
- + "<td>"+lastname+"</td>"
- + "</tr>";
- $("#result" ).html(content);
- });
- });
- var xml =
- "<?xml version='1.0' ?>"
- + "<doc>"
- +" <user>"
- +" <firstname>Tony</firstname>"
- +" <lastname>Stark</lastname>"
- +" </user>"
- +" <user>"
- +" <firstname>Claire</firstname>"
- +" <lastname>Temple</lastname>"
- +" </user>"
- +" <user>"
- +" <firstname>John</firstname>"
- +" <lastname>Smith</lastname>"
- +" </user>"
- +"</doc>";
- });
Add new comment
- 365 views