• We just launched and are currently in beta. Join us as we build and grow the community.

How to Display XML Data using jQuery

Deific

Anime Quizmaster
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
174
Likes
133
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Display XML Data using jQuery</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <button

    type

    =

    "button"

    class

    =

    "btn btn-success"

    id

    =

    "display"

    >

    Display Data</

    button

    >
  18. <br

    /

    ><br

    /

    >
  19. <table

    class

    =

    "table table-bordered"

    >
  20. <thead

    >
  21. <tr

    >
  22. <th

    >

    Firstname</

    th

    >
  23. <th

    >

    Lastname</

    th

    >
  24. <th

    >

    Age</

    th

    >
  25. </

    tr

    >
  26. </

    thead

    >
  27. <tbody

    id

    =

    "result"

    style

    =

    "background-color:#fff;"

    ></

    tbody

    >
  28. </

    table

    >
  29. </

    div

    >
  30. </

    body

    >
  31. <script

    src

    =

    "js/jquery-3.2.1.min.js"

    ></

    script

    >
  32. <script

    src

    =

    "script.js"

    ></

    script

    >
  33. </

    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. let xml =
  3. "<?xml version='1.0' ?>"
  4. +

    "<doc>"
  5. +

    " <member>"
  6. +

    " <firstname>John</firstname>"
  7. +

    " <lastname>Smith</lastname>"
  8. +

    " <age>21</age>"
  9. +

    " </member>"
  10. +

    " <member>"
  11. +

    " <firstname>Steve</firstname>"
  12. +

    " <lastname>Roger</lastname>"
  13. +

    " <age>25</age>"
  14. +

    " </member>"
  15. +

    " <member>"
  16. +

    " <firstname>Natasha</firstname>"
  17. +

    " <lastname>Romanof</lastname>"
  18. +

    " <age>19</age>"
  19. +

    " </member>"
  20. +

    " <member>"
  21. +

    " <firstname>Yor</firstname>"
  22. +

    " <lastname>Forger</lastname>"
  23. +

    " <age>26</age>"
  24. +

    " </member>"
  25. +

    " <member>"
  26. +

    " <firstname>Tony</firstname>"
  27. +

    " <lastname>Stark</lastname>"
  28. +

    " <age>35</age>"
  29. +

    " </member>"
  30. +

    " </doc>"

    ;


  31. $(

    '#display'

    )

    .on

    (

    'click'

    ,

    function

    (

    )

    {
  32. let data =

    $.parseXML

    (

    xml)

    ;

  33. let $xml =

    $(

    data)

    ;

  34. let $member =

    $xml.find

    (

    "member"

    )

    ;

  35. let content =

    ""

    ;
  36. $member.each

    (

    function

    (

    )

    {

  37. let firstname =

    $(

    this

    )

    .find

    (

    'firstname'

    )

    .text

    (

    )

    ;
  38. let lastname =

    $(

    this

    )

    .find

    (

    'lastname'

    )

    .text

    (

    )

    ;
  39. let age =

    $(

    this

    )

    .find

    (

    'age'

    )

    .text

    (

    )

    ;

  40. content +=

    "<tr>"
  41. +

    "<td>"

    +

    firstname+

    "</td>"
  42. +

    "<td>"

    +

    lastname+

    "</td>"
  43. +

    "<td>"

    +

    age+

    "</td>"
  44. +

    "</tr>"

    ;

  45. $(

    "#result"

    )

    .html

    (

    content)

    ;

  46. }

    )

    ;
  47. }

    )

    ;


  48. }

    )

    ;

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:

How%20to%20Display%20XML%20Data%20using%20jQuery%201.png


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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,695

323,213

323,222

Top