1

I'm trying to write an app using World Weather Online services. The request from their website gives me a weather code telling the type of weather. This value is a number corresponding a description in a XML file which looks like this:

<condition>
    <code>119</code>
    <description>Cloudy</description>
    <day_icon>
      wsymbol_0003_white_cloud</day_icon>
    <night_icon>
      wsymbol_0004_black_low_cloud
    </night_icon>
    </condition>
  <condition>
    <code>116</code>
    <description>Partly Cloudy</description>
    <day_icon>wsymbol_0002_sunny_intervals</day_icon>
    <night_icon>wsymbol_0008_clear_sky_night</night_icon>
  </condition>
  <condition>
    <code>113</code>
    <description>Clear/Sunny</description>
    <day_icon>wsymbol_0001_sunny</day_icon>
    <night_icon>wsymbol_0008_clear_sky_night</night_icon>
  </condition>

Using this XML file gives me the advantage of making translations for other languages. What I need is to search for the code and get the description into a variable for later use.

How do I do this using jQuery?

/Carl

1 Answer 1

1

Working demo http://jsfiddle.net/JtCrH/ or http://jsfiddle.net/usFtT/

API you are looking for is:

Rest should fit your needs :)

code

$(document).ready(function () {
    var str = "<xml version='1.0'><condition><code>119</code><description>Cloudy</description><day_icon>wsymbol_0003_white_cloud</day_icon><night_icon>wsymbol_0004_black_low_cloud</night_icon></condition></xml>";
    var xmlDoc = $.parseXML(str);
    var $xml = $(xmlDoc);
    var $Name = $xml.find('code');
    var $Description = $xml.find('description');
    $('span').html($Name);
    $('span').append($Description);
});
Sign up to request clarification or add additional context in comments.

4 Comments

Maybe I haven't explained myself properly. I have the <code> value, ex. 119, and I wan't to search for the <description> that fits the code.
Ah, okies, in thatcase see here: stackoverflow.com/questions/6542187/… Change it into an array and then find the matching code and grab the description node. this should defo fit your cause. :)
Trying this: var tmpCode, tmpDescription; var weatherDescription = new Array(); $.ajax({ url: 'wwoConditionCodes.xml', type: 'GET', dataType: 'xml', success: function(returnedXMLResponse){ $('condition', returnedXMLResponse).each(function() { tmpCode = $('code', this).text(); tmpDescription = $('description', this).text(); weatherDescription[tmpCode] = tmpDescription; }) } }); The array doesn't seem to work. How to display the description using for example weatherDescription[119] to display "Cloudy".
Sorry. I'm pretty new at using stackoverflow so I don't know how to format the text in comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.