8

Using JavaScript/Ajax?

I'm trying to extract values from:

<yweather:astronomy sunrise="6:34 am"   sunset="8:38 pm"/>

Looking for something like:

var response = transport.responseXML.getElementsByTagName("channel");
sunrise = response[0].getElementsByTagName("yweather:astronomy").item(0).Attributes["sunrise"].Value;

But nothing works so far. :'( Thanks.

1 Answer 1

9

There is a special version of getElementsByTagName for namespaces: getElementsByTagNameNS.

For example:

var response = transport.responseXML.getElementsByTagName("channel");
var sunrise = response[0].getElementsByTagNameNS("[Namespace URI]", "astronomy")[0].getAttribute("sunrise");

...where [Namespace URI] is the URI of the yweather namespace.

Sign up to request clarification or add additional context in comments.

4 Comments

@annakata: Out of interest, how can do you do it in IE?
I think you can just use getElementsByTagName("yweather:astronomy") with IE. Also, Google feeds API has a cross browser implementation. Maybe you can use that, or something like it: code.google.com/apis/ajaxfeeds/documentation/…
In MSXML, you can set the "SelectionNamespaces" property on the XML DOMDocument object, then use the non-standard "selectNodes" (or "selectSingleNode") method with an XPath selector. See xml.com/pub/a/2002/06/05/msxml4.html for an example.
You might need to escape the colon "yweather\\:astronomy"

Your Answer

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