みんな
さて、yahoo weather api から情報を取得する天気アプリケーションを作成しています。XML は次のとおりです: http://weather.yahooapis.com/forecastrss?p=44310
ps XML ファイルとして表示されるため、Google Chrome 内で開くことをお勧めします。
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;