Results 1 to 5 of 5

Thread: logic for comparing parsed xml

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default logic for comparing parsed xml

    i am parsing XML using jQuery like this
    Code:
    $(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:
    Code:
    <?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
    Code:
    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?
    Code:
    function parseXml(xml){
      $(xml).find("entry").each(function(){
        $("#output").append($(this).attr("name") + 
    	" | " + 
    	$(this).find("email").text() + 
    	" | " + 
    	$(this).find("score").text() +"<br />");
      });
    
    }
    Last edited by ggalan; 08-19-2011 at 05:50 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Something like so should work:

    Code:
    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 />");
        }
      });
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    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!!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    .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:

    Code:
    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ggalan (08-19-2011)

  6. #5
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    wow, i never knew!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •