msu msu - 16 days ago 5
jQuery Question

Jquery retrieve object value

I read this xml and trying to get different nodes value. I can just look for a specific node and get the value but the problem is there are multiple nodes with the same name. So when I get the value it gives me all the nodes value that I specified. I want to get each node value separately. Any help will be appreciated. Thanks in advance.

Here is the xml:

<LogLine>
<DateTime>2016-11-17T16:48+0000</DateTime>
<Operation>Register1</Operation>
</LogLine>
<LogLine>
<DateTime>2016-11-17T16:48+0000</DateTime>
<Operation>Register2</Operation>
</LogLine>
<LogLine>
<DateTime>2016-11-17T16:48+0000</DateTime>
<Operation>Register3</Operation>
</LogLine>


What I have done

success: function(xml) {
var xmlDoc = jQuery.parseXML(xml);
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$dateTime = $xml.find( "DateTime" );
$( "#xmlElement" ).append( "Log In Information: "+$dateTime .text() );
}


HTML:

Current output:
2016-11-17T18:31+00002016-11-17T18:31+00002016-11-17T18:31+0000

Answer

This seems to work,

Your XML was initially invalid, I've added LINES, part..

var xml = `<Lines>
  <LogLine>
     <DateTime>2016-11-17T16:48+0000</DateTime>
     <Operation>Register1</Operation>
  </LogLine>
  <LogLine>
     <DateTime>2016-11-17T16:48+0000</DateTime>
     <Operation>Register2</Operation>
  </LogLine>
  <LogLine>
     <DateTime>2016-11-17T16:48+0000</DateTime>
     <Operation>Register3</Operation>
  </LogLine></Lines>`;

$(function () {
  var xmlDoc = $($.parseXML( xml ));
  xmlDoc.find('Lines LogLine').each(function () {
    var $t = $(this);
    console.log(
      $t.find('DateTime').text() + '  ' +
      $t.find('Operation').text()  
    );
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments