0

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

5
  • 1
    i think $dateTime is an array, try selecting the specific value like this: $dateTime[0].text() Commented Nov 18, 2016 at 11:02
  • Getting an error, $dateTime[0].text() it's not a function. $dateTime[0] it is an element that's for sure but not sure how to get the value of this element. Sorry don't have that much experience with JQuery. Commented Nov 18, 2016 at 11:08
  • @msu try following code i put Commented Nov 18, 2016 at 11:18
  • @TechBreak I getting error , $.parseXML(....)..find is not a function Commented Nov 18, 2016 at 11:21
  • @msu updated, try now Commented Nov 18, 2016 at 11:22

2 Answers 2

2

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>

Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

 success: function(xml) {
    var resultXML = $.parseXML(xml);
    //loop over each 'LogLine' element node
    $(resultXML).find('LogLine').each(function(index){
       //within each 'LogLine' node find 'DateTime' node
       var dateTime = $(this).find( "DateTime" );
       $( "#xmlElement" ).append( "Log In Information:    "+dateTime.text() );
    }
 }

4 Comments

That looks better :). One more thing how can I add a new line here, $( "#xmlElement" ).append( "Log In Information: "+dateTime .text() ); I tried this, $("#xmlElement").append( "Log In Information: "+dateTime.text() +"\n" ); but it did not like it.
You need to append <br/> tag if you are appending to DOM.
@msu i hope my answer fixed your issue. Please accept.
@msu glad to help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.