开发者

How to Structure E4X Syntax to handle Attributes?

I'm using the weather.com XML web service to get the forecast high temp, low temp and icon. The XML snippet is as follows:

<dayf>
<lsup>5/14/11 2:21 AM Local Time</lsup> 
<day d="0" t="Saturday" dt="May 14">
<hi>63</hi> 
<low>48</low> 
<sunr>6:39 AM</sunr> 
<suns>5:04 PM</suns> 
<part p="d">
<icon>32</icon> 
开发者_开发知识库

I'm attempting to trace the above nodes and attributes using the following script snippet:

XML.ignoreWhitespace = true;
var urlLoader:URLLoader = event.currentTarget as URLLoader;
var resultXML:XML = XML(urlLoader.data);
trace(resultXML.dayf.day.hi);
trace(resultXML.dayf.day.low);
trace(resultXML.dayf.day.part.attribute("p=d").icon);

lbllowtemp.text= String(resultXML.dayf.day.low);
lblhitemp.text=String(resultXML.dayf.day.hi);
uicondicon.source=String(resultXML.dayf.day.part.attribute("p=d").icon)+".png";

However, when I run this script in my Flash application, I receive:

Error #2044: Unhandled ioError:. text=Error #2035: URL Not Found. URL: file:///C|/Documents/WeatherFLV/.png

So, the hi and low values are being read but the icon value in node part with attribute p=d is not working.

How should I modify my E4X syntax to obtain the icon node?

Thanks much!


The attribute name is 'p' and not 'p=d', and the value of attribute p is "d". So you want to find the icon node within a <part> tag with an attribute p who has the value "d".

.(xxxxx) will filter your XML with a given condition, here @p is the attribute

if you have only one part with such a value you can do:

resultXML.dayf.day.part.(@p=="d").icon

otherwise if you have multiple value you can get the first record:

resultXML.dayf.day.part.(@p=="d")[0].icon
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜