luiquao
luiquao

Reputation: 1114

Better way to parse

I want to parse this code to get the values

Code to parse

Currently I do

$(user).find('x item').each(function () {
    var first =  $(this).context.getElementsByTagName('value')[3].childNodes[0]['textContent'];
});

just to get "Sam" from Sam

I am sure there is a better and more reliable way do the same. I also want to find a way to avoid this kind of errors if any of the values are missing:

TypeError: $(...).context.getElementsByTagName(...)[2].childNodes[0] is undefined

Upvotes: 0

Views: 51

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

Since you are getting the exception I'm guessing the $(user).find('x item') selector is fine. So you can try

$(user).find('x item').each(function () {
      var first =  $(this).find('field[var="first"] value').text();
});

Upvotes: 3

Related Questions