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

The importance of CSS styling links: the difference between a:link, a:visited and simply using a

Curious about the difference between using: a { ... } versus a:link, a:visited { ... } ...

Creating a visually appealing table in HTML or experimenting with a tableless design can greatly enhance your website's layout

Presenting data with headers in HTML has been a challenge for me. Below is the portion of the header I'm having difficulty with. Although I've successfully rotated the text, the issue I'm facing now is text overlap. Here's the code st ...

Extract data from input field and transfer to another page using ajax without needing to submit the form

My form includes an email input field: <input type="email" id="email" name="email"/> There is also a verify button: <span style="cursor:pointer"> <p id="verify">Verify</p> </span> Upon clicking the verify button, a new in ...

Is it possible to incorporate Google icons within Freemarker?

https://i.stack.imgur.com/Pv2T4.pngI'm having trouble using Google icons in my project. Can anyone help me figure out how to fix this? UPDATE: Here is a snippet of my HTML template: <?xml version="1.0" encoding="UTF-8"?> < ...

Parsing text files with Highcharts

As a beginner in JavaScript and HighCharts, I am facing a simple problem that has me completely lost. My goal is to generate a scatter chart with three lines by reading data from a text file formatted like this: x y1 y2 y3 1.02 1.00 6.70 ...

The VueJS Hello world application is experiencing technical difficulties and is currently not functioning as

As a complete VueJS newbie, I've just started working on my very first app. It's supposed to be a simple Hello World application, but unfortunately, the code isn't quite behaving as expected :( HTML <!DOCTYPE html> <html lang=" ...

I'm in need of assistance with Laravel 6 to implement a search functionality that can search for users by name, specialty, and country

Whenever I click the Search button, an error (Undefined nom) pops up. I am looking for someone who can help me troubleshoot and fix my code. Here is the code that needs correction: Controller: doctors.php <?php namespace App\Http\Cont ...

Issue with jQuery control not appearing on WinPhone 8.1 browser (Internet Explorer II)

When I wanted to familiarize myself with MVC4, I decided to create a simple default Internet Application using Visual Studio 2012. As part of the project, I integrated a jQuery-based countdown control and added a text box with a datepicker feature: <s ...

What could be causing the issue of Vuejs 3.3 defineModel consistently returning undefined?

I am currently working with Nuxt version 3.5.1 and Vuejs version 3.3, however, I am encountering an issue where the defineModel macro always returns undefined. I am unsure why this is happening? <template> <input v-model="count"& ...

How to merge text (string) with date in HTML using JavaScript

I am attempting to blend a day with the phrase "this is the day" in HTML. I gave it a shot, but the concatenation didn't work as expected. ...

Create a payment invoice using the power of AJAX, jQuery, PHP, and MySQL

Having issues with my bill calculation. When adding multiple products, the price of all products appears to be the same. I'm unable to calculate the total correctly and struggling to fix this. Please review my code and provide insights. bill.php < ...

Obtain the numerical value of the vertical position of the mouse (

Currently, I am coding a JavaScript game and my objective is to designate a variable specifically for the Y axis of the mouse. I kindly request that the code be kept as simple and straightforward as possible, avoiding unnecessary complexity. That conclud ...

I'm looking for a way to dynamically update Laravel pagination records based on the selected option value for the number of items per page using Laravel and

I am working on creating a custom filter system using a select option menu and pagination with AJAX. The select option allows users to choose between displaying 10, 15, 20, or 25 products per page while updating the Laravel default pagination dynamically ...

Using JavaScript parameters in a HTML document

I am trying to replicate a page similar to this. The issue I am facing is the inability to use external JS files in ASP.net (as far as I know). Therefore, I am defining the functions and attempting to utilize them within the HTML page instead. <%@ P ...

What is preventing my accordion from closing completely?

I'm currently working on creating FAQ questions in an accordion format. However, I've encountered an issue where adding padding around the answer section prevents the accordion from closing completely. Removing the padding solves the problem, but ...

Ways to obtain the chosen option from a drop-down menu using jQuery

I'm currently experiencing an issue where the selected value of a drop down is not being displayed correctly. Instead of selecting the dropdown value, it's adding another value to the list. Below is the code snippet I'm using: JQuery var ...

Shift the checkbox label over to the left with just CSS

I am faced with the following code snippet: <div> <span> <input type="checkbox"> <label>I desire this to appear on the left-hand side</label> </span> </div> My goal is to shift the label to the left side of ...

Tips for creating responsive jQuery autocomplete list items

I've created a website using Bootstrap 4 and integrated JQuery autocomplete. However, the list items in the autocomplete feature are not responding well to different screen sizes. Take a look at my code below: jQuery(document).ready(function ($) { ...

Utilize jQueryUI sortable to serialize a list item within an unordered list

I'm looking to learn how to generate a JSON or serialize from a ul element with nested list items. Here's an example: <ul class="menu send ui-sortable"> <li id="pageid_1" class="ui-sortable-handle">Inscription <ul class="menu ...

Simple Steps to Connect an HTML File to CSS in Windows Notepad

I have scoured the depths of the online realm in search of a method to connect HTML with CSS within the preinstalled Notepad application on Windows. Regrettably, my attempts, including utilizing the system path, proved fruitless. Is it conceivable to ach ...