2009-07-05 17 views
7

Uso di JavaScript/Ajax?Analisi di spazi dei nomi XML?

sto cercando di estrarre i valori da:

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

cercando qualcosa di simile:

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

Ma non funziona nulla finora. : '( Grazie

risposta

8

Esiste una versione speciale di getElementsByTagName per i namespace:.. getElementsByTagNameNS

Ad esempio:.

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

... dove [Namespace URI] è l'URI del namespace yweather

Steve

+2

Non supportato da IE, ma correggere altrimenti – annakata

+0

@annakata: Per interesse, come puoi farlo in IE? –

+0

Penso che tu possa semplicemente usare getElementsByTagName ("yweather: astronomy") con IE. Inoltre, l'API dei feed di Google ha un'implementazione cross-browser. Forse puoi usare quello, o qualcosa del genere: http://code.google.com/apis/ajaxfeeds/documentation/reference.html#getElementsByTagNameNS – dylanfm

Problemi correlati