大家
好的,所以我正在创建一个天气应用程序,它从雅虎天气api获取信息。这是XML:http://weather.yahooapis.com/forecastrss?p=44310
P.S。最好在谷歌浏览器中打开它,因为它会将其显示为XML文件。
我正在尝试从XML中获取多个项目。我试图让信息显示完整的城市和州,但我该怎么做?
XML适用于我所在的地区,但我需要它才能与任何城市合作。
这是我的代码:
public class PanelBase extends libs.PanelBase
{
private var vo:WeatherVO;
private var _details:DetailsView;
public function PanelBase()
{
super();
// below is my settings for the input area where you have to enter your zip code
// .text empties the field, .restrict keeps a person from using what ever
// is not added. in this case, the person can only use numbers from 0 to 9 only
// .maxchars sets the number of possible items that a person can enter inside the input field.
// buttonMode just creates the illusion of a button formed from a graphic
this.tf_Zipcode.text = "";
this.tf_Zipcode.restrict = "0-9";
this.tf_Zipcode.maxChars = 5;
this.btn_Enter.addEventListener(MouseEvent.CLICK, processZip);
}
// function for the button
private function processZip(e:Event):void
{
// if the zip code area is wrong, inside of the text area above the zip code
// will let you know that the zip entered is wrong, but if the zip code
// you enter has less than 5 numbers, then you will be told there aren't enough numbers
// then the zip code text field will empty itself.
if(this.tf_Zipcode.text == null)
{
this.tf_Hint.text = "Sorry! Your zip code is invalid.";
}
else if(this.tf_Zipcode.text.length < 5)
{
this.tf_Zipcode.text = "";
this.tf_Hint.text = "There are not enough numbers!"
}
else
{
this.removeChild(this.tf_Zipcode);
this.addChild(this.tf_Zipcode);
this.tf_Zipcode.text = "";
//getImages();
loadInfo();
}
}
// function for loading the XML
private function loadInfo():void
{
var ld:URLLoader = new URLLoader();
ld.load(new URLRequest("http://weather.yahooapis.com/forecastrss?p=44310&u=f"));
ld.addEventListener(Event.COMPLETE, parseInfo);
}
// function
private function parseInfo(e:Event):void
{
var xmlData:XML = XML(e.target.data);
var ns1:Namespace = new Namespace("http://xml.weather.yahoo.com/ns/rss/1.0");
var ns2:Namespace = new Namespace("http://xml.weather.yahoo.com/ns/rss/1.0");
// parsing the information from the XML into the text fields
vo = new WeatherVO();
vo.City = xmlData.channel.ns1::location.@city;
vo.CurrentTemp = xmlData.channel.ns1::condition.@temp;
vo.High = xmlData.item.channel.ns1::forecast.@high;
vo.Low = xmlData.item.channel.ns1::forecast.@low;
vo.Humidity = xmlData.channel.ns1::atmosphere.@humidity;
vo.Wind = xmlData.channel.ns1::wind.@chill;
vo.Rise = xmlData.channel.ns1::astronomy.@sunrise;
vo.Set = xmlData.channel.ns1::astronomy.@sunset;
vo.Tomorrow = xmlData.channel.item.ns2::forecast[1].@day;
trace(xmlData)
updateTextFields();
}
private function updateTextFields():void
{
//remove zip code view and then add result panel view
_details = new DetailsView();
this.addChild(_details);
_details.tf_City.text = vo.City;
_details.tf_CurrentTemp.text = vo.CurrentTemp;
_details.tf_High.text = vo.High;
_details.tf_Low.text = vo.Low;
_details.tf_Humidity.text = vo.Humidity;
_details.tf_Wind.text = vo.Wind;
_details.tf_Rise.text = vo.Rise;
_details.tf_Set.text = vo.Set;
_details.tf_Tomorrow.text = vo.Tomorrow;
答案 0 :(得分:1)
尝试将以下内容添加到parseInfo方法中:
vo.Region = xmlData.channel.ns1::location.@region;
trace (vo.City + ", " + vo.Region);