开发者

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>&lt;?xml version="1.0" encoding="utf-16"?&gt;

&lt;LocalWeather xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;

&lt;AreaType&gt;suburb&lt;/AreaType&gt;

&lt;AreaId&gt;555&lt;/AreaId&gt;

&lt;AreaName&gt;SYDNEY&lt;/AreaName&gt;

&lt;UTCOffset&gt;600&lt;/UTCOffset&gt;

&lt;ForecastCreated&gt;2011-04-29T17:10:25Z&lt;/ForecastCreated&gt;

&lt;ObservationsCreated&gt;2011-04-29T17:00:00Z&lt;/ObservationsCreated&gt;

&lt;NowItems&gt;

&lt;NowItem&gt;

  &lt;Label&gt;CURRENT TEMPERATURE&lt;/Label&gt;

  &lt;Value&gt;16.7&lt;/Value&gt;

  &lt;Units&gt;°C&lt;/Units&gt;

  &lt;Change&gt;STEADY&lt;/Change&gt;

&lt;/NowItem&gt;

&lt;NowItem&gt;

  &lt;Label&gt;FEELS LIKE&lt;/Label&gt;

  &lt;Value&gt;16.9&lt;/Value&gt;

  &lt;Units&gt;°C&lt;/Units&gt;

  &a开发者_C百科mp;lt;Change&gt;STEADY&lt;/Change&gt;

&lt;/NowItem&gt;

&lt;NowItem&gt;

  &lt;Label&gt;DEW POINT&lt;/Label&gt;

  &lt;Value&gt;16&lt;/Value&gt;

  &lt;Units&gt;°C&lt;/Units&gt;

  &lt;Change&gt;DOWN&lt;/Change&gt;

&lt;/NowItem&gt;

&lt;NowItem&gt;

  &lt;Label&gt;HUMIDITY&lt;/Label&gt;

  &lt;Value&gt;96&lt;/Value&gt;

  &lt;Units&gt;%&lt;/Units&gt;

&lt;/NowItem&gt;

&lt;NowItem&gt;

  &lt;Label&gt;WIND SPEED&lt;/Label&gt;

  &lt;Value&gt;9&lt;/Value&gt;

  &lt;Units&gt;km/h&lt;/Units&gt;

  &lt;Change&gt;STEADY&lt;/Change&gt;

  &lt;Direction&gt;SW&lt;/Direction&gt;

&lt;/NowItem&gt;

&lt;NowItem&gt;

  &lt;Label&gt;WIND GUSTS&lt;/Label&gt;

  &lt;Value&gt;15&lt;/Value&gt;

  &lt;Units&gt;km/h&lt;/Units&gt;

&lt;/NowItem&gt;

&lt;NowItem&gt;

  &lt;Label&gt;PRESSURE&lt;/Label&gt;

  &lt;Value&gt;1022&lt;/Value&gt;

  &lt;Units&gt;hPa&lt;/Units&gt;

  &lt;Change&gt;STEADY&lt;/Change&gt;

&lt;/NowItem&gt;

&lt;NowItem&gt;

  &lt;Label&gt;RAIN SINCE 9AM&lt;/Label&gt;

  &lt;Value&gt;9&lt;/Value&gt;

  &lt;Units&gt;mm&lt;/Units&gt;

  &lt;Change&gt;N/A&lt;/Change&gt;

&lt;/NowItem&gt;

&lt;NowItem&gt;

  &lt;Label&gt;FIRE DANGER LEVEL&lt;/Label&gt;

  &lt;Value&gt;UNAVAILABLE&lt;/Value&gt;

  &lt;Units&gt;N/A&lt;/Units&gt;

&lt;/NowItem&gt;

&lt;/NowItems&gt;

&lt;WMOID&gt;94768&lt;/WMOID&gt;

&lt;SiteId&gt;66062&lt;/SiteId&gt;

&lt;WeatherStation&gt;SYDNEY&lt;/WeatherStation&gt;
&lt;WeatherToday&gt;

&lt;Icon&gt;Fewshowers&lt;/Icon&gt;

&lt;FriendlyName&gt;Few showers&lt;/FriendlyName&gt;

&lt;Precis&gt;Few showers.&lt;/Precis&gt;

&lt;MaxTemp&gt;22&lt;/MaxTemp&gt;

&lt;MinTemp&gt;17&lt;/MinTemp&gt;

&lt;Units&gt;°C&lt;/Units&gt;

&lt;MorningIcon&gt;Showers&lt;/MorningIcon&gt;

&lt;AfternoonIcon&gt;Fewshowers&lt;/AfternoonIcon&gt;

&lt;EveningIcon&gt;Showers&lt;/EveningIcon&gt;

&lt;MorningPrecis&gt;Showers&lt;/MorningPrecis&gt;

&lt;AfternoonPrecis&gt;Few showers&lt;/AfternoonPrecis&gt;

&lt;EveningPrecis&gt;Showers&lt;/EveningPrecis&gt;

&lt;/WeatherToday&gt;

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:

&lt 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜