JavaScript Lessons

JavaScript Parse XML: Show Data in an HTML Table

Use XMLHttpRequest to load XML data, parse it with the DOM, and render the results as a clean HTML table, step by step.

Serhat ALPASLAN3 min read

Listing data with JavaScript and XML

Listing data with JavaScript using XML (eXtensible Markup Language) makes web pages dynamic and efficient. In this article, we’ll explain how to list data with JavaScript using XML and the technical details behind it.

XML is a text-based data format used to store data through tags that describe it. Reading and processing data with JavaScript using XML makes web pages dynamic. As a result, the web page updates dynamically even without the user changing anything.

To read XML data with JavaScript, you use the XMLHttpRequest object. This object is used to retrieve and process data from a web server asynchronously. For example, the following code block fetches the “data.xml” file from the web server and places its contents into an HTML div element called “myDiv”:

 

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    document.getElementById("myDiv").innerHTML = this.responseText;
  }
};
xhttp.open("GET", "data.xml", true);
xhttp.send();

In this code block, you first create an XMLHttpRequest object. Using the object’s “onreadystatechange” property, you define what should happen in a particular state. In this case, when readyState is 4 and status is 200, the contents of the HTML div element called “myDiv” are updated with the responseText property. The “open” and “send” methods are used to request the “data.xml” file from the web server.

To make XML data easier to process with JavaScript, you can use a “DOM Parser”. This is a structure used to store data in a tree. The following code block reads the “data.xml” file and gets the “title” and “description” elements of each “item” element and prints them to the screen:

var parser, xmlDoc;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(this.responseText,"text/xml");
    var items = xmlDoc.getElementsByTagName("item");
    for (var i = 0; i < items.length; i++) {
        var title = items[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
        var description = items[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
        console.log(title + " - " + description);
    }
  }
};
xhttp.open("GET", "data.xml", true);
xhttp.send();

In this code block, the XML data is parsed with the DOM Parser. The “title” and “description” elements between the “item” tags are accessed. The contents of these elements are printed to the screen.

This article has provided an overview of listing data with JavaScript using XML. We explained the XMLHttpRequest object and the use of the DOM Parser, which are needed to implement these operations. Once you learn how to use these techniques, you’ll have the tools you need to build dynamic and efficient web pages.

The “data.xml” file must be an XML file used to store the data. As an example , you can use a file structure like the following:

<?xml version="1.0" encoding="UTF-8"?>
<items>
  <item>
    <title>Item 1</title>
    <description>Description of Item 1</description>
  </item>
  <item>
    <title>Item 2</title>
    <description>Description of Item 2</description>
  </item>
  <item>
    <title>Item 3</title>
    <description>Description of Item 3</description>
  </item>
</items>

In this file structure, the data is stored between the “item” tags inside the “items” tag. Within each “item” tag, the “title” and “description” tags are used. The “title” tag contains the title of the data, and the “description” tag contains the description of the data. Real applications can contain more tags and data.

 

Share it if it helped

Write a comment