0

我正在尝试从 free.worldweatheronline.com 的 xml Web 服务获取数据,如果在 Web 服务中找到城市,我会给出城市名称,它会返回如下数据:

<data><request><type>City</type><query>Hyderabad, india</query></request><current_condition><observation_time>06:04 AM</observation_time><temp_C>34</temp_C><temp_F>92</temp_F><weatherCode>113</weatherCode><weatherIconUrl>http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png</weatherIconUrl><weatherDesc>Sunny</weatherDesc><windspeedMiles>14</windspeedMiles><windspeedKmph>22</windspeedKmph><winddirDegree>230</winddirDegree><winddir16Point>SW</winddir16Point><precipMM>0.0</precipMM><humidity>50</humidity><visibility>10</visibility><pressure>1011</pressure><cloudcover>0</cloudcover></current_condition>

我的 http 服务弹性代码是这样的:

<s:HTTPService id="weatherService"
                   url="{BASE_URL}"
                   resultFormat="object"
                   result="weatherService_resultHandler(event)"
                   fault="weatherService_faultHandler(event)"
                   showBusyCursor="true">
        <s:request xmlns="">
            <q>{cityName.text.toString()}</q>
            <format>{FORMAT}</format>
            <num_of_days>{NUMBER_OF_DAYS}</num_of_days>
            <key>{API_KEY}</key>
        </s:request>
    </s:HTTPService>

这是处理代码:

private static const BASE_URL:String="http://free.worldweatheronline.com/feed/weather.ashx";
private static const API_KEY:String="MY_API_KEY";
private static const NUMBER_OF_DAYS:uint=2;
private static const FORMAT:String="xml";


protected function weatherService_resultHandler(event:ResultEvent):void
{
    // TODO Auto-generated method stub
    var result_weather_data:Object = event.result;
    cityNameData.text=result_weather_data.data.request.query;
}

protected function weatherService_faultHandler(event:FaultEvent):void
{
    // TODO Auto-generated method stub          

}

如果未找到该城市,则返回如下 xml 数据:

<data><error><msg>Unable to find any matching weather location to the query submitted!</msg></error></data>

我正在尝试这样做

var error_msg:String = result_weather_data.data.error.msg; 

在上面代码中的 cityNameData.text 之前,但它给出了未定义属性或类似内容的错误

4

1 回答 1

0

如果 xml 中不存在属性,则会引发错误

//还要注意根名不需要指定访问这个子节点

//您需要像这样检查XML中的属性是否可用

        if(event.result is XML)
    {           
        var errorXML:XML = event.result as XML; 

        if(errorXML && errorXML.hasOwnPerperty("error") )
        {
            if(errorXML.error && errorXML.error.hasOwnProperty("msg"))
            {
                var yourErrorMsg:String = errorXML.error.msg; 
            }
        }
    }           
    else if(event.result is Object)
    {
        var result_weather_data:Object = event.result;

        if(result_weather_data && result_weather_data.hasOwnProperty('data'))
        {
            if(result_weather_data.data && result_weather_data.data.hasOwnProperty("request"))
            {
                if(result_weather_data.data.request && result_weather_data.data.request.hasOwnProperty("query"))
                {
                    cityNameData.text = result_weather_data.data.request.query;
                }
                else
                {
                    trace("query property not exists in result_weather_data.data.request Object");
                }
            }
            else
            {
                trace("request property not exists in result_weather_data.data Object");
            }
        }
        else
        {
            trace("result_weather_data is NULL or data property not exists in result_weather_data Object");
        }
    }
于 2012-09-26T08:06:08.053 回答