Skip to main content
Inspiring
September 28, 2009
Answered

Parsing XML Namespace in as2

  • September 28, 2009
  • 1 reply
  • 986 views

HI, I am attempting to parse some weather information from the Yahoo weather RSS feed.

This is a typical XML node;

<description>Yahoo! Weather for Palm Springs, CA</description>

I used the typical XML parsing code in as2;

var text1 = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;

txt1.text = text1;

I was doing pretty good until I reached a node that was formatted thus;

<yweather:forecast day="Mon" date="28 Sep 2009" low="74" high="103" text="Sunny" code="32" />

This text traced a value of undefined.

I have tried to research this and so far all I have been able to find out, is that this is a XML namespace and is very easily handled in as3. Unfortunately I have to use as2.

How do I parse out this information into a dyamic text fields?

Or failing that, where in the literature does adobe expalin how this is done?

Forrest

This topic has been closed for replies.
Correct answer

Hi,

Try this code:

var xmlname:XML = new XML();

xmlname.ignoreWhite = true;

xmlname.load("sample.xml");

xmlname.onLoad = function(sucess:Boolean){

var xNode = xmlname.firstChild;

var tday = xNode.childNodes[0].attributes.day;

var tdate = xNode.childNodes[0].attributes.date;

trace(tdate);

}

1 reply

Correct answer
September 29, 2009

Hi,

Try this code:

var xmlname:XML = new XML();

xmlname.ignoreWhite = true;

xmlname.load("sample.xml");

xmlname.onLoad = function(sucess:Boolean){

var xNode = xmlname.firstChild;

var tday = xNode.childNodes[0].attributes.day;

var tdate = xNode.childNodes[0].attributes.date;

trace(tdate);

}

GumpsterFAuthor
Inspiring
September 29, 2009

Yes, that works perfectly!

Thanks Saransoft84.

You also showed me a great way of shortening my as code.