PDA

View Full Version : Resolved logic for comparing parsed xml



ggalan
08-18-2011, 04:44 PM
i am parsing XML using jQuery like this


$(document).ready(function(){
$.ajax({
type: "GET",
url: "list.xml",
dataType: "xml",
success: parseXml,
error: err
});
});
function err(xhr, reason, ex) { $('#output').append(reason); }

function parseXml(xml){
$(xml).find("entry").each(function(){
$("#output").append($(this).attr("name") +
" | " +
$(this).find("email").text() +
" | " +
$(this).find("score").text() +"<br />");
});

}

my xml:


<?xml version="1.0" encoding="iso-8859-1"?>
<data>
<entry name="John Doe">
<email>john.doe@yahoo.com</email>
<score>88</score>
<Date>1/13/2009</Date>
</entry>
<entry name="GI Jane">
<email>g.jane@gmail.com</email>
<score>104</score>
<Date>1/13/2011</Date>
</entry>
<entry name="Joe Smith">
<email>joe.s@hotmail.com</email>
<score>100</score>
<Date>9/13/2010</Date>
</entry>
</data>


my output looks like


John Doe | john.doe@yahoo.com | 88
GI Jane | g.jane@gmail.com | 104
Joe Smith | joe.s@hotmail.com | 100


question: i want to output only those that score 100 or above
what is the best way to go about this? should i push elements into an array and compare those results?
can i somehow write the comparison into the parsing?


function parseXml(xml){
$(xml).find("entry").each(function(){
$("#output").append($(this).attr("name") +
" | " +
$(this).find("email").text() +
" | " +
$(this).find("score").text() +"<br />");
});

}

jscheuer1
08-19-2011, 04:23 AM
Something like so should work:


function parseXml(xml){
$(xml).find("entry").each(function(){
if($(this).find("score").text() > 99){
$("#output").append($(this).attr("name") +
" | " +
$(this).find("email").text() +
" | " +
$(this).find("score").text() +"<br />");
}
});
}

ggalan
08-19-2011, 03:02 PM
that worked! i thought the values from xml were strings but you were able to compare them as int, how was that possible?
also i thought maybe you needed to use .val

but comparing ".text() > 99" thats so straightforward!!

jscheuer1
08-19-2011, 05:31 PM
.val() is only for the value attribute/property of form elements. It could possibly be used in other situations, but the results might not be cross browser. In any case, it also returns a string.

And you're right about comparing a string to a number, weird isn't it? In javascript, when using < or >, if the string can be type converted to a number it will be automatically. Same for ==, or !=, but not for === or !==, the latter two tell javascript not to attempt type conversion, and so save time if you know the two things being compared are of the same type.

Often when automatic type conversion to a number is being relied upon we do something like:


var nstr = $(this).find("score").text();
if(!isNaN((nstr = +nstr)){
// treat nstr as a number here, because it is one now
}

Generally I think even with a string that couldn't be converted, you'd be OK because:


alert('dog' > 99);

alerts false. It would skip that one anyway. And that would be fine, unless you wanted to trap the error and make some kind of alert about it or have something written to the output area about it.

ggalan
08-19-2011, 05:49 PM
wow, i never knew!