Integrate XML information into a webpage's table layout

I am currently working on integrating an XML file into my HTML and displaying it in a table. A snippet of the XML is provided below:

<?xml version="1.0" encoding="UTF-8"?>
<product category="DSLR" sub-category="Camera Bundles">
    <id>0001</id>
    <title>Canon EOS Rebel T6 DSLR Camera Bundle with EF-S 18-55mm f/3.5-5.6 IS II Lens + Canon EF 75-300mm f/4-5.6 III Lens</title>
    <brand>Canon</brand>
    <price>399.99</price>
    <description>The Canon EOS Rebel T6 DSLR Camera Bundle allows you to capture stunning moments with exceptional quality. This compact camera features an 18-megapixel CMOS sensor, delivering sharp images even in low-light conditions. With an ISO range of 100 to 6400 (expandable to 12800), you can take clear and detailed photos in any lighting. The DIGIC 4+ image processor ensures fast performance and vibrant colors, making every photo memorable.</description>
</product>

Based on the ID, title, brand, price, and description tags, I have attempted to write jQuery code to link them to the table, but I am facing difficulties in making it work.

$(document).ready(function() {
  $.ajax({
    type: "GET",
    url: "productsXmlStructure.xml",
    dataType: "xml",
    success: function(xml){
      $('#t01').append('<th>ID</th>'); 
      $('#t01').append('<table id="show_table">'); 
      $(xml).find('show').each(function(){
        var $show = $(this);
        var id = $show.find('ID').text();
        var title = $show.find('Title').text();
        var brand = $show.find('Brand').text();
        var price = $show.find('Price').text();
        var desc = $show.find('Description').text();
      });
    }
  });
});

I have limited knowledge in this language, but I have included my table structure below. I would appreciate any assistance in identifying where I may be going wrong.

<table id="t01" border="2" style="width:75%" align="center">
  <tr>
    <th>ID</th>
    <th>Title</th>
    <th>Brand</th>
    <th>Price</th>
    <th>Description</th>
  </tr>
</table>

Answer №1

  1. Opt for using filter in place of find
  2. Choose product over show (as per the XML)
  3. Append a row to #t01 for each occurrence of product.
  4. Add a cell to the above row for each property with the corresponding data.

var xml = '<?xml version="1.0" encoding="UTF-8"?> <product top-level-category="DSLR" sub-level-category="Camera Bundles"> <id>0001</id> <title>NIKON D3300 DSLR Camera with 18-55 mm f/3.5-5.6 II ED Zoom Lens – BlacK</title> <brand>Nikon</brand> <price>279.00</price> <description>The Nikon D3300 DSLR Camera with 18-55 mm f/3.5-5.6 II ED Zoom Lens allows you to capture special moments in glorious high quality. Unforgettable memories, unforgettable photos. Small and lightweight, the D3300 has a 24.2 megapixel, 23.5 x 15.6 mm CMOS sensor with remarkable light sensitivity that produces amazingly sharp images. It performs well in low light with an ISO range of 100 to 12800 (extendable to 25600). Your images will be packed with fine textures and natural colours to really capture the atmosphere of whichever situation you find yourself in. Capture clear, colourful photographs in all manner of lighting conditions as the camera\'s EXPEED 4 image processor goes to work, delivering gorgeous photos every time.</description></product>';

$(xml).filter('product').each(function(){
  var $show = $(this);
  var data = {
    id: $show.find('ID').text(),
    title: $show.find('Title').text(),
    brand: $show.find('Brand').text(),
    price: $show.find('Price').text(),
    desc: $show.find('Description').text()
  };

  var row = $('<tr />');
  for (var prop in data) {
    $('<td>' + data[prop] + '</td>').appendTo(row);  
  }
  
  $('#t01').append(row);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="t01" table border="2" style="width:75%" table align="center">
  <tr>
    <th>ID</th>
    <th>Title</th>
    <th>Brand</th>
    <th>Price</th>
    <th>Description</th>
  </tr>
</table>

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

What is the best way to create a JavaScript "input" field that only accepts vowel letters?

Is there a way to create an "input" field that only accepts vowel letters? Additionally, I want to ensure that copying and dragging text into the input field will also be filtered out. <div class="field"> <input class="fie ...

Tips on how to pass properties as arguments to mixins

Here is the code I have: mox(first, second) {first} second a mox(transition, 0.3s linear all) I want to call the mixin like this: mox(transition 0.3s linear all) If we modify mox, it would look like this: mox() arguments a mox transition 0.3 ...

Issue with wp_ajax function not being triggered in Wordpress

Attempting to implement my first AJAX Function in Wordpress 4.3. I created a plugin called "calipso". Within the plugin, there are two files: calipso.php : <?php /** * @package calipso * @version 1.0 */ /* Plugin Name: calipso Plugin URI: http://www. ...

The stick division shows some bouncy behavior when seen on a mobile device

When viewing on mobile, the #main-categories div seems to be jumping instead of smoothly sticking to the top. What could be causing this behavior? Here is the code snippet I have: var s = $("#main-categories"); var pos = s.position(); $(window).scroll(f ...

Issues arise when using the "placeholder" attribute within a <p:inputText tag while the inputText is devoid of any value

A current project involves implementing JSF Mojarra 2.3.9.SP02 alongside PrimeFaces 7.0 on Wildfly 17 using the Sapphire template provided by PrimeFaces. However, I've encountered a significant issue with a specific <p:inputText element within my f ...

Firefox not clearing Highcharts points properly

I am currently utilizing highcharts to generate dynamic charts when hovering over a table row. My goal is to clear and hide the chart once the mouse moves away from the row. While this functionality works smoothly in Chrome, I am encountering a strange is ...

My goal is to create a stylish form using bootstrap and CSS3

I am aiming to replicate the layout of this particular page: Struggling to utilize css3 for designing, how can I achieve a form similar to this? Facing difficulty in creating the outer red border and inserting spacing between fields. Seeking advice on th ...

Tips for ensuring that the horizontal scroll bar remains consistently positioned at the bottom of the screen

There are two div sections on a page, with one positioned on the left and the other on the right. The div on the right contains multiple dynamically generated tags which necessitate a horizontal scroll bar (overflow:auto). This causes the div's height ...

Setting a constant width for text characters across all devices using CSS and HTML

I am looking to display text in my textarea with the same width in pixels across all devices. Essentially, I want to set the character width in pixels so that it appears consistently on any device (Desktop/Mobile). <html> <head> <styl ...

Modify the specified pattern with regex only if it does not appear in a particular location in the content

Our tool development heavily relies on regex for various functionalities. One key aspect involves replacing placeholders with actual values dynamically using standard JavaScript's `RegExp`. All placeholder formats are similar to this: {{key}} In mo ...

When words are enclosed in a table cell, the fixed table layout ensures that all columns are the same size

There are times when long words in my HTML table cells need to be broken if they overflow their designated area. To learn how to break table cell words, check out this question: Word-wrap in an HTML table The recommended CSS style is: style="word-wrap: ...

Incorporate a pseudo class to a unique custom template or directive within an Angular project

I have been developing a custom dropdown menu directive in AngularJS. The issue I'm facing is that the buttons in my template become inactive when interacting with the dropdown menu, unlike a regular HTML select which remains active while the dropdown ...

Tips for confining a float within a div with fluctuating space

I have a scenario where I have multiple divs ('group') containing text, and there is a floated div ('toggle') in the bottom corner. The current code works well if the text length in 'group' is consistent, but the position of t ...

Ways to display multiple PHP pages in a single division

Within my project, I have a unique setup involving three distinct PHP pages. The first file contains two divisions - one for hyperlinked URLs and the other for displaying the output of the clicked URL. Here is an excerpt from the code snippet: <script& ...

Is the item positioned above another item instead of being positioned to the left of it?

Looking for help with my mobile navigation setup. I want to add text that says ' ...

Utilizing jQuery's then() method to display HTML and JSON outputs from printing operations

Currently, I am in the process of mastering jquery deferreds. When working with html on jsfiddle, I receive an object that contains two lines when printed within the then() statement: "success" and the html returned by the ajax request. For instance, upo ...

How can I retrieve the width of a responsive React element during its initial rendering phase?

In my React project, there is a component called ResultList which is used to display products in a gallery format. The challenge I'm facing is determining the appropriate number of products to show per row based on the available width for the ResultL ...

Adjust the height value of each element to match the maximum height within a single line using only CSS

There are 3 divs styled with display:inline-block. I aim to adjust their height values so they match the highest one. The desired effect is demonstrated visually below. Is it doable using only CSS? ----- ----- ----- ----- ----- ---- ...

Incorporating JSON objects into MySQL through a For Each loop - Checkbox Selections

My attempt to update the table (auth_users) with checkbox values using QueryBuilder and the provided code is not functioning correctly for the checkboxes on my form. I suspect that the issue lies in $key, "'".$value."'" not matching the DB field ...

How to implement CSS styling for a disabled component

In my application, I have a FormControlLabel and Switch component. When the Switch is disabled, the label in FormControlLabel and the button in Switch turn gray. However, I want to maintain the color of both the label (black) and the button (red). To test ...