Ajay Y Ajay Y - 1 month ago 11
Javascript Question

I want to access the attribute of xml tag using javascript, how I can access it?

I am getting the XML data from link "http://weather.yahooapis.com/forecastrss?p=CHXX0008&u=unit"

Following is my javscript code :

$.ajax({
type: "GET",
url: "http://weather.yahooapis.com/forecastrss?"+"p="+zipcode+"&u=c",
dataType: "xml",
success: function(xml) {
$(xml).find('channel').each(function(){

}
} } );


enter image description here

Using above code I am trying to access "city" attribute of tag "yweather:location". How can I accesss? Please check the XML data using link provided.

Answer
success: function(xml) {
                var loc = xml.getElementsByTagNameNS('http://xml.weather.yahoo.com/ns/rss/1.0', 'location');
                var city = loc[0].getAttribute('city');
                console.log(city);
            } 

Hope this helps you. Mark as answer if you are satisfied.