What is the best way to extract information from an XML file using JQUERY and present it neatly in a table format

I am looking to populate a table with data from an XML file as shown below:

<table>
<th>Head 1</th><th>Head 2</th><th>Head 3</th>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
<tr><td></td><td></td><td></td></tr>
</table>

All three columns of the table should be filled with unique data from the XML file. I have attempted this, but I can only see one data entry from the file instead of all rows being filled with distinct values. Also, once new XML files are added to the folder, the old XML data should be replaced entirely by the new XML data.

Below is the HTML and jQuery code snippets that I have implemented. HTML File

<div>
    <table class="status_table">
        <th class="table_heading">Heading 1</th>
        <th class="table_heading" style="text-align:center; padding:30px;">Heading 2</th>
        <th class="table_heading" style="text-align:center; padding:30px;">Heading 3</th>
        <tr></tr>
    </table>
</div>

jQuery File

$('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />');

$(document).ready(function(){
    truckData();
    fetch();
})
function fetch(){
    setTimeout(function(){
        truckData();
        fetch();
    },100)
}

function truckData(){
$.ajax({
  url: "20200707083610.xml",
  dataType: "",
  success: function(data){

    $("tr").children().remove();
    $(data).find("DisplayData").each(function(){
      var info = '<td class="table_content" style="color: #DB075C;">'+$(this).find("LaneName").text()+'<td class="table_content" style="color: #FFFF00;">'+$(this).find("PlateNumber").text()+'<td class="table_content" style="color: #3107DB;">'+$(this).find("BayName").text();

      $("tr").append(info);
    });
  }
})
}

XML File

<DisplayRequestData>
        <DisplayData displayPort="d-01">
            <LaneName>Lane 1</LaneName>
            <PlateNumber>7709</PlateNumber>
            <BayName>ABCD 1</BayName>
        </DisplayData>
        <DisplayData displayPort="d-02">
            <LaneName>Lane 2</LaneName>
            <PlateNumber>5652</PlateNumber>
            <BayName>XYZA 0012</BayName>
        </DisplayData>
        <DisplayData displayPort="d-03">
            <LaneName>Lane 3</LaneName>
            <PlateNumber>XR-20398</PlateNumber>
            <BayName></BayName>
        </DisplayData>
</DisplayRequestData>

Answer №1

If you want to add new rows to your table, consider using += instead of simply appending rows in the tr. It would be more efficient to use tbody and append trs within this tbody element. Additionally, remember to include dataType: "xml" in your ajax call if you are returning an xml file as a response.

Example Code:

// Sample XML data
var data = '<DisplayRequestData><DisplayData displayPort="d-01"><LaneName>Lane 1</LaneName><PlateNumber>7709</PlateNumber><BayName>ABCD 1</BayName> </DisplayData><DisplayData displayPort="d-02"><LaneName>Lane 2</LaneName><PlateNumber>5652</PlateNumber><BayName>XYZA 0012</BayName></DisplayData> <DisplayData displayPort="d-03"><LaneName>Lane 3</LaneName> <PlateNumber>XR-20398</PlateNumber> <BayName></BayName></DisplayData></DisplayRequestData>'
var info = "";
// Parse XML (not required if response is specified as XML)
$xml = $($.parseXML(data));
// Loop through the data
$xml.find("DisplayData").each(function() {
  // Append rows
  info += '<tr><td class="table_content" style="color: #DB075C;">' + $(this).find("LaneName").text() + '<td class="table_content" style="color: #FFFF00;">' + $(this).find("PlateNumber").text() + '<td class="table_content" style="color: #3107DB;">' + $(this).find("BayName").text() + '</tr>';
});
// Add rows inside tbody
$("table tbody").html(info);
LaneName PlateNumber BayName

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experiencing a problem with a jQuery dropdown navigation bar

After spending roughly 3 hours researching this issue to no avail, I have decided to reach out for help. I have created a jsFiddle that I hope someone can take a look at and provide guidance on. The problem I am facing involves a simple dropdown menu that ...

Tap the image to open it in a new window for a closer look

After retrieving data from the database, which includes images and other fields, I am trying to figure out how to make it so that when users click on an image, it opens in a new window. Below is the code snippet I have: <a target="_blank" href="#"> ...

Two entities positioned on opposite extremes

Is there a way to design a div with two elements (text and image) positioned on opposite sides? The length of the text varies as it's a list of months for selection. Ideally, the text should appear on the left side of the div as a "p" element, while t ...

Determine the vertical dimension of a child division once it has been integrated into an HTML document

My goal is to create a website with multiple pages without having to recreate the toolbar for each page. To achieve this, I have created a separate HTML file and CSS file specifically for the toolbar. On each page, I simply import the toolbar using the fo ...

Create an interactive musical experience on a webpage using HTML

I'm a newcomer to web technology and I'm trying to dynamically play a music file using Python Django HTML. I have saved the file location in the database and am fetching it. <audio control><source src= {{track.songlocation}} type="audio ...

Async functions within async functions

I am trying to obtain the geolocation data from a client and then load locations using Ajax, followed by displaying them as a list. Within my code, I have three functions: getGeolocation, loadLocation, and createList. Both getGeolocation and loadLocation ...

Importing PNG files through AJAX and rendering them on a canvas using a Flask server

How can I render a PNG image received via an AJAX call in a canvas? A video file (mp4) is uploaded via AJAX to a Flask server. The first frame is extracted using Cv2, saved, and then returned to the client. I am able to retrieve the PNG data as a string, ...

Differences in Loading Gif Visualization Across Chrome, Safari, and Firefox

Having an issue with a loading image in Chrome, while it displays correctly in Safari and Firefox. In Chrome, I see two half-loading images instead of one whole image. Struggling to find a solution for this problem, any assistance would be greatly apprecia ...

Tips for modifying the height of a bootstrap-vue table and enabling scrolling with a fixed header

I have a div containing a b-table that I need help adjusting the height for. I want to make the table scrollable with a fixed header. Can anyone assist me? Here is my code: Code inside the template: <div class="tbl-barangay"> <b-table stripe ...

Enhance your viewing experience with the Zoom feature that activates when

I recently noticed on that I am able to zoom/enlarge a photo by clicking on it. Is there a way for me to incorporate this feature without purchasing the entire theme? While I understand the theme is designed for purchase, I only need this specific functi ...

What is the best way to place a button within a line of text

I am trying to achieve a layout similar to this: -------------------button------------------ I can add text inside the line, but it doesn't look good when I add a button. Any suggestions on how I can improve this? ...

Using jQuery to make an AJAX request to a localhost API running on port

I created a RESTful API within a folder named "rest" that connects to port 3000 on my localhost at http://localhost:3000/api/products. Additionally, I developed a jQuery logging application in a folder named "logs" that makes Ajax calls to retrieve data fr ...

IE browser not sending ajax request with jQuery's $.getJSON method (works fine in FF and Chrome)

While using IE9's debug mode, I placed a breakpoint on the first line below and it was hit successfully. However, the breakpoint set on the first line in the callback function is never triggered. Upon checking the Network tab, I noticed that there is ...

There seems to be an issue with Select2 where the selected value is not

I have upgraded to version 4.0.0 full in order to take advantage of the features available in 3.5.2 as well. I am utilizing the query option to populate the results. The functionality is working properly, however, when I choose an item from the results, it ...

The removeClass method does not affect the class attribute when using $(this).attr('class'), but only when using $(this).attr('id')

I am currently facing an issue with reducing the size of my jQuery code. The main function is to validate a form - if empty, I want to add a class of 'highlight' or remove it using addClass('highlight') and removeClass('highlight&a ...

What is the specific function of jQuery Custom events and how do they operate

I've been on the hunt for reliable sources that delve into the inner workings of custom events in jQuery. Specifically, I want to understand how they replicate event bubbling and more. ...

Client-side dynamic model binding

For my asp.net MVC razor page project, I am facing the challenge of dynamically adding and deleting models using jQuery on the client side. The requirement is to be able to add/delete models at any position in the list, not just at the end. How can I manag ...

The responsiveness of Angular Material appears to be limited when tested in Chrome's debug mode for different devices

I have come across a peculiar issue. Within my HTML file, I have defined two attributes: Hide me on small devices Hide me on larger than small devices When I resize the window, the attributes work as intended. However, when I enter device debug mode ( ...

Can a Dashcode Webkit Web app be transformed into traditional HTML and CSS?

I have developed a blog using Dashcode, incorporating HTML, CSS, and Javascript to pull data from an xml file. It's pretty simple... My perspective on this is: 1. JavaScript should be compatible with all browsers (with some variations). 2. I may need ...

The button fails to trigger the AJAX request

I've developed a form that includes a QR code generator. The QR code is generated as an SVG, and below the code is a download button that should trigger an AJAX call to my PHP script when clicked. However, the download button does not seem to initiate ...