How to Parse XML Nodes that have Spaces in the Node Label?
I'm using the weather.com web service to request current conditions for a location, and am parsing the current temperature, feels like temp and current condition icon to my Flash application. The Current Temperature and Feels Like Temperature nodes are nested within NowItems -> NowItem while WeatherToday is further along in the feed. I've written the following in AS3:
public class Main extends MovieClip
{
public function Main()
{
var urlRequest:URLRequest = new URLRequest("WebServiceURL");
var urlLoader:URLLoader=new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, onXmlLoaded);
urlLoader.load(urlRequest);
}
private function onXmlLoaded(event:Event):void
{
XML.ignoreWhitespace = true;
var urlLoader:URLLoader = event.currentTarget as URLLoader;
var resultXML:XML = XML(urlLoader.data);
trace(resultXML.NowItems.NowItem.CURRENT TEMPERATURE.Value);
trace(resultXML.NowItems.NowItem.FEELS LIKE.Value);
trace(resultXML.WeatherToday.Icon);
var lowtemp_label :TextField = new TextField();
addChild(lowtemp_label);
var hitemp_label :TextField=new TextField();
addChild(hitemp_label);
var condicon_label :TextField=new TextField();
addChild(condicon_label);
lowtemp_label.text = resultXML.minTemp;
hitemp_label.text= resultXML.maxTemp;
condicon_label.text=resultXML.icon;
}
}
Here's some of the XML:
<rss version="2.0" xmlns:a10="http://www.w3.org/2005/Atom">
<channel>
<title>WEATHERSCAPE</title>
<link>http://www.weatherchannel.com.au/</link>
<description>WEATHERSCAPE DATA</description>
<category>Weather</category>
<item>
<guid isPermaLink="false">18b88e0b-b53f-41a3-bdfb-0762ae440f60</guid>
<link>http://www.weatherchannel.com.au/</link>
<title>Weather</title>
<description><?xml version="1.0" encoding="utf-16"?>
<LocalWeather xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AreaType>suburb</AreaType>
<AreaId>555</AreaId>
<AreaName>SYDNEY</AreaName>
<UTCOffset>600</UTCOffset>
<ForecastCreated>2011-04-29T17:10:25Z</ForecastCreated>
<ObservationsCreated>2011-04-29T17:00:00Z</ObservationsCreated>
<NowItems>
<NowItem>
<Label>CURRENT TEMPERATURE</Label>
<Value>16.7</Value>
<Units>°C</Units>
<Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>FEELS LIKE</Label>
<Value>16.9</Value>
<Units>°C</Units>
&a开发者_C百科mp;lt;Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>DEW POINT</Label>
<Value>16</Value>
<Units>°C</Units>
<Change>DOWN</Change>
</NowItem>
<NowItem>
<Label>HUMIDITY</Label>
<Value>96</Value>
<Units>%</Units>
</NowItem>
<NowItem>
<Label>WIND SPEED</Label>
<Value>9</Value>
<Units>km/h</Units>
<Change>STEADY</Change>
<Direction>SW</Direction>
</NowItem>
<NowItem>
<Label>WIND GUSTS</Label>
<Value>15</Value>
<Units>km/h</Units>
</NowItem>
<NowItem>
<Label>PRESSURE</Label>
<Value>1022</Value>
<Units>hPa</Units>
<Change>STEADY</Change>
</NowItem>
<NowItem>
<Label>RAIN SINCE 9AM</Label>
<Value>9</Value>
<Units>mm</Units>
<Change>N/A</Change>
</NowItem>
<NowItem>
<Label>FIRE DANGER LEVEL</Label>
<Value>UNAVAILABLE</Value>
<Units>N/A</Units>
</NowItem>
</NowItems>
<WMOID>94768</WMOID>
<SiteId>66062</SiteId>
<WeatherStation>SYDNEY</WeatherStation>
<WeatherToday>
<Icon>Fewshowers</Icon>
<FriendlyName>Few showers</FriendlyName>
<Precis>Few showers.</Precis>
<MaxTemp>22</MaxTemp>
<MinTemp>17</MinTemp>
<Units>°C</Units>
<MorningIcon>Showers</MorningIcon>
<AfternoonIcon>Fewshowers</AfternoonIcon>
<EveningIcon>Showers</EveningIcon>
<MorningPrecis>Showers</MorningPrecis>
<AfternoonPrecis>Few showers</AfternoonPrecis>
<EveningPrecis>Showers</EveningPrecis>
</WeatherToday>
I get compiler errors on the lines requesting the Current Temperature and Feels Like, as they have spaces in them? What syntax do I use to reference these nodes given that they have spaces? Is this the correct way to reference these nodes, or are there simpler methods? When I reference the nodes in my label text (last 3 lines), do I use the fully qualified node as is in my trace statements?
Thanks much!!
Inside of the description node is an XML STRING that is not XML
you need to get the description node then convert it to xml to parse it
[EDIT]
OK, what you have in your example is basically 2 XML files one inside the other.
So what we need to do is parse out the description node you can learn how to do that from HERE and HERE is a good example of a loader
Once you have successfully obtained the contents of the description node you now have to unescape the data because it has been converted to html friendly format.(pretty sure the unescape function will handle this if not post another question on here about how to convert it - sorry no time to test right now)
Example:
< should be converted to <
After the string has been converted you will create a new XML: object off of that and then access the node you want like you did when you parsed the server response.
精彩评论