0

I'm using AJAX to load an XML file in to a HTML div. My XML looks like this:

<Incidents>
  <Incident id="123">
    <ModTime>2016-01-23T08:00:00Z</ModTime>
    <comments>comments here</comments>
    <currentUpdate>update</currentUpdate>
    <status>Active</status>
  </incident>
  <Incidents>
    <Incident id="456">
      <ModTime>2016-01-23T08:00:00Z</ModTime>
      <comments>comments here</comments>
      <currentUpdate>update</currentUpdate>
      <status>Active</status>
    </incident>
//<![CDATA[
$.ajax({
  type: "GET",
  url: "feed.xml",
  dataType: "xml",
  success: function(xml) {
    $('#table').append('<h2>XML</h2>'); 
    $('#table').append('<table id="show_table" border="1" >'); 
    $(xml).find('Incident').each(function() {
      var $feed = $(this);
      var id = $feed.attr("id");
      var mod = $feed.find('ModTime').text();
      var comments = $feed.find('comments').text();
      var update = $feed.find('currentUpdate').text();
      var status = $feed.find('status').text();
      var html = ' <tr><td >' + id + '</td><td >' + mod + '<br>' + comments     + '</td><td>' + update + '</td><td>'  + status +'</td><td > </tr>';
      $('#show_table').append(html);
    });
  }  
});

Which works fine most of the time, however sometimes incidents in the XML will have two comment sections.

<Incidents>
  <Incident id="123">
    <ModTime>2016-01-23T08:00:00Z</ModTime>
    <comments>comments here</comments>
    <currentUpdate>update</currentUpdate>
    <status>Active</status>
  </incident>
  <Incidents>
    <Incident id="456">
      <ModTime>2016-01-23T08:00:00Z</ModTime>
      <comments>comments here</comments>
      <comments>comments another comment</comments>
      <currentUpdate>update</currentUpdate>
      <status>Active</status>
    </incident>

When that happens I want to separate them out but I can't work out how?

I first tried adding a line break in the table but realized that was too late as I'd already got the data.

I then tried adding the break when I find the comments but that also didn't work - it just added a break at the end of the

var comments = $feed.find('comments').text() + '<br>';

2
  • 1
    That XML seems to have some syntax issues Commented Feb 18, 2019 at 10:59
  • That's my simplifying the XML - the XML I'm getting works fine. This stackoverflow.com/questions/26118696/… seems to be very similar to my problem. I just need to work out how to adapt it to my table rather than a list. Commented Feb 19, 2019 at 11:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.