MXMLTest08 External XML Read Test (前回MXMLTest07からのレイアウト変更です)
forked from MXMLTest07 External XML Read Test (外部XMLから画像&リンクURLを取得処理) (diff: 281)
ActionScript3 source code
/**
* Copyright siouxcitizen ( http://wonderfl.net/user/siouxcitizen )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tuYV
*/
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="500" height="500" backgroundColor="#333333">
<mx:Script>
<![CDATA[
//Yahooショッピングから"出石そば"で検索した結果(XML)を処理
//前回よりも少々データの詳細が表示されるように変更しました。
//スクロールバーも表示されるようにしました。
//前回から予定していたDataGridの使用は重そうなのでやめました。
//そしてベタベタコーディングがどんどんひどくなっていく。。。
//ベタベタだから本来コンピュータがやるべきループ仕事を人力でやっているようなもの。。。
//そろそろ人力も限界っぽい。。。
//ループでデータ渡せるようにしなければ。。。
//下のほうにあるコンボボックスの値を追加、または変更すれば検索キーワードの追加・変更が可能です
//検索結果ページ遷移機能もつけようとしたけれど力尽きました。。。 結局レイアウト変えただけかも。
private var query:String = "出石そば";//検索キーワード デフォルト値設定
private var URL:String; //クエリ先URL
private var loader:URLLoader;
private var xmlData:XML;
private var ns:Namespace = new Namespace("urn:yahoo:jp:itemSearch");
private var preName:String = "タイトル : ";
private var preHeadline:String = "ここがウリ! : ";
private var prePrice:String = "お値段 : ";
private var preReview:String = " レビューポイント : ";
private var preReviewCnt:String = "レビュー数 : ";
private var link00:String; //画像リンク先保持用
private var link01:String;
private var link02:String;
private var link03:String;
private var link04:String;
private var link05:String;
private var link06:String;
private var link07:String;
private var link08:String;
private var link09:String;
private var link10:String;
private var link11:String;
private var link12:String;
private var link13:String;
private var link14:String;
private var link15:String;
//クエリ先URLと検索キーワードを結合後、クエリ先URLへ検索を行う
private function testIamgeDisplay():void {
//外部XML処理中はボタンを不活性化
btnTest.enabled = false;
//URL+クエリ情報設定
URL = "http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch?appid=uQ_E1kmxg64.0fo1fkMwTnvcEmBn.foaHDXp0MF8WvUXD3jQcDaDIAuC0b6zRFmh3BT2umN6&query=" + escapeMultiByte(query);
var request:URLRequest = new URLRequest(URL);
request.url = URL;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, completeHandler);
try {
loader.load(request);
} catch (error:Error) {
trace("Unable to load requested document.");
}
}
//クエリ先より取得したXMLを処理
private function completeHandler(event:Event):void {
xmlData = new XML(loader.data);
var count:uint = 0;
default xml namespace = ns; //名前空間設定
//XMLデータ保持用
var name:String;
var headline:String;
var price:String;
var review:String;
var reviewCnt:String;
var imgURL:String;
var linkURL:String;
//取得した外部XMLから画像&リンク先URLを取り出して処理
for each(var element:Object in xmlData.Result.Hit){
name = element.Name;
headline = element.Headline;
price = element.Price;
review = element.Review.Rate;
reviewCnt = element.Review.Count;
imgURL = element.Image.Small;
linkURL = element.Url;
//画像&リンク先URLを設定する
ctrlDispInfo(count, name, headline, price, review, reviewCnt, imgURL, linkURL);
count++;
}
btnTest.enabled = true; //外部XML処理後にボタンを活性化
}
//変更されたコンボボックスの値をセット
private function onChangeCombo():void {
query = String(comboTest.selectedItem);
}
//外部XMLから取得した画像URLとリンク先URLを設定
private function ctrlDispInfo(count:uint, name:String, headline:String, price:String, review:String, reviewCnt:String, imgURL:String, linkURL:String):void{
switch (count) {
case 0:
label00.text = preName + name;
headline00.text = preHeadline + headline;
price00.text = prePrice + price;
review00.text = preReviewCnt + reviewCnt + preReview + review;
image00.source = imgURL;
link00 = linkURL;
break;
case 1:
label01.text = preName + name;
headline01.text = preHeadline + headline;
price01.text = prePrice + price;
review01.text = preReviewCnt + reviewCnt + preReview + review;
image01.source = imgURL;
link01 = linkURL;
break;
case 2:
label02.text = preName + name;
headline02.text = preHeadline + headline;
price02.text = prePrice + price;
review02.text = preReviewCnt + reviewCnt + preReview + review;
image02.source = imgURL;
link02 = linkURL;
break;
case 3:
label03.text = preName + name;
headline03.text = preHeadline + headline;
price03.text = prePrice + price;
review03.text = preReviewCnt + reviewCnt + preReview + review;
image03.source = imgURL;
link03 = linkURL;
break;
case 4:
label04.text = preName + name;
headline04.text = preHeadline + headline;
price04.text = prePrice + price;
review04.text = preReviewCnt + reviewCnt + preReview + review;
image04.source = imgURL;
link04 = linkURL;
break;
case 5:
label05.text = preName + name;
headline05.text = preHeadline + headline;
price05.text = prePrice + price;
review05.text = preReviewCnt + reviewCnt + preReview + review;
image05.source = imgURL;
link05 = linkURL;
break;
case 6:
label06.text = preName + name;
headline06.text = preHeadline + headline;
price06.text = prePrice + price;
review06.text = preReviewCnt + reviewCnt + preReview + review;
image06.source = imgURL;
link06 = linkURL;
break;
case 7:
label07.text = preName + name;
headline07.text = preHeadline + headline;
price07.text = prePrice + price;
review07.text = preReviewCnt + reviewCnt + preReview + review;
image07.source = imgURL;
link07 = linkURL;
break;
case 8:
label08.text = preName + name;
headline08.text = preHeadline + headline;
price08.text = prePrice + price;
review08.text = preReviewCnt + reviewCnt + preReview + review;
image08.source = imgURL;
link08 = linkURL;
break;
case 9:
label09.text = preName + name;
headline09.text = preHeadline + headline;
price09.text = prePrice + price;
review09.text = preReviewCnt + reviewCnt + preReview + review;
image09.source = imgURL;
link09 = linkURL;
break;
case 10:
label10.text = preName + name;
headline10.text = preHeadline + headline;
price10.text = prePrice + price;
review10.text = preReviewCnt + reviewCnt + preReview + review;
image10.source = imgURL;
link10 = linkURL;
break;
case 11:
label11.text = preName + name;
headline11.text = preHeadline + headline;
price11.text = prePrice + price;
review11.text = preReviewCnt + reviewCnt + preReview + review;
image11.source = imgURL;
link11 = linkURL;
break;
case 12:
label12.text = preName + name;
headline12.text = preHeadline + headline;
price12.text = prePrice + price;
review12.text = preReviewCnt + reviewCnt + preReview + review;
image12.source = imgURL;
link12 = linkURL;
break;
case 13:
label13.text = preName + name;
headline13.text = preHeadline + headline;
price13.text = prePrice + price;
review13.text = preReviewCnt + reviewCnt + preReview + review;
image13.source = imgURL;
link13 = linkURL;
break;
case 14:
label14.text = preName + name;
headline14.text = preHeadline + headline;
price14.text = prePrice + price;
review14.text = preReviewCnt + reviewCnt + preReview + review;
image14.source = imgURL;
link14 = linkURL;
break;
case 15:
label15.text = preName + name;
headline15.text = preHeadline + headline;
price15.text = prePrice + price;
review15.text = preReviewCnt + reviewCnt + preReview + review;
image15.source = imgURL;
link15 = linkURL;
break;
default:
break;
}
}
//画像クリック時、指定されたリンク先にとばす
private function naviToURL(imgNum:String):void{
switch (imgNum) {
case "0":
navigateToURL(new URLRequest(link00));
break;
case "1":
navigateToURL(new URLRequest(link00));
break;
case "2":
navigateToURL(new URLRequest(link02));
break;
case "3":
navigateToURL(new URLRequest(link03));
break;
case "4":
navigateToURL(new URLRequest(link04));
break;
case "5":
navigateToURL(new URLRequest(link05));
break;
case "6":
navigateToURL(new URLRequest(link06));
break;
case "7":
navigateToURL(new URLRequest(link07));
break;
case "8":
navigateToURL(new URLRequest(link08));
break;
case "9":
navigateToURL(new URLRequest(link09));
break;
case "10":
navigateToURL(new URLRequest(link10));
break;
case "11":
navigateToURL(new URLRequest(link11));
break;
case "12":
navigateToURL(new URLRequest(link12));
break;
case "13":
navigateToURL(new URLRequest(link13));
break;
case "14":
navigateToURL(new URLRequest(link14));
break;
case "15":
navigateToURL(new URLRequest(link15));
break;
default:
break;
}
}
]]>
</mx:Script>
<mx:Button id="btnTest" click="testIamgeDisplay();" x="10" y="10" label="検索" enabled="true"/>
<mx:ComboBox id="comboTest" x="70" y="10" change="onChangeCombo()">
<mx:ArrayCollection>
<mx:String>出石そば</mx:String>
<mx:String>但馬牛</mx:String>
<mx:String>ズワイガニ</mx:String>
</mx:ArrayCollection>
</mx:ComboBox>
<mx:VBox x="10" y="50" height="400" width="450">
<mx:HBox>
<mx:Canvas>
<mx:Image id="image00" click="naviToURL('0')"/>
<mx:Label x="80" y="0" id="label00" text="" color="white"/>
<mx:Text x="80" y="20" id="headline00" text="" color="white"/>
<mx:Text x="80" y="40" id="price00" text="" color="white"/>
<mx:Text x="80" y="60" id="review00" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image01" click="naviToURL('1')"/>
<mx:Label x="80" y="0" id="label01" text="" color="white"/>
<mx:Text x="80" y="20" id="headline01" text="" color="white"/>
<mx:Text x="80" y="40" id="price01" text="" color="white"/>
<mx:Text x="80" y="60" id="review01" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image02" click="naviToURL('2')"/>
<mx:Label x="80" y="0" id="label02" text="" color="white"/>
<mx:Text x="80" y="20" id="headline02" text="" color="white"/>
<mx:Text x="80" y="40" id="price02" text="" color="white"/>
<mx:Text x="80" y="60" id="review02" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image03" click="naviToURL('3')"/>
<mx:Label x="80" y="0" id="label03" text="" color="white"/>
<mx:Text x="80" y="20" id="headline03" text="" color="white"/>
<mx:Text x="80" y="40" id="price03" text="" color="white"/>
<mx:Text x="80" y="60" id="review03" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image04" click="naviToURL('4')"/>
<mx:Label x="80" y="0" id="label04" text="" color="white"/>
<mx:Text x="80" y="20" id="headline04" text="" color="white"/>
<mx:Text x="80" y="40" id="price04" text="" color="white"/>
<mx:Text x="80" y="60" id="review04" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image05" click="naviToURL('5')"/>
<mx:Label x="80" y="0" id="label05" text="" color="white"/>
<mx:Text x="80" y="20" id="headline05" text="" color="white"/>
<mx:Text x="80" y="40" id="price05" text="" color="white"/>
<mx:Text x="80" y="60" id="review05" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image06" click="naviToURL('6')"/>
<mx:Label x="80" y="0" id="label06" text="" color="white"/>
<mx:Text x="80" y="20" id="headline06" text="" color="white"/>
<mx:Text x="80" y="40" id="price06" text="" color="white"/>
<mx:Text x="80" y="60" id="review06" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image07" click="naviToURL('7')"/>
<mx:Label x="80" y="0" id="label07" text="" color="white"/>
<mx:Text x="80" y="20" id="headline07" text="" color="white"/>
<mx:Text x="80" y="40" id="price07" text="" color="white"/>
<mx:Text x="80" y="60" id="review07" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image08" click="naviToURL('8')"/>
<mx:Label x="80" y="0" id="label08" text="" color="white"/>
<mx:Text x="80" y="20" id="headline08" text="" color="white"/>
<mx:Text x="80" y="40" id="price08" text="" color="white"/>
<mx:Text x="80" y="60" id="review08" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image09" click="naviToURL('9')"/>
<mx:Label x="80" y="0" id="label09" text="" color="white"/>
<mx:Text x="80" y="20" id="headline09" text="" color="white"/>
<mx:Text x="80" y="40" id="price09" text="" color="white"/>
<mx:Text x="80" y="60" id="review09" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image10" click="naviToURL('10')"/>
<mx:Label x="80" y="0" id="label10" text="" color="white"/>
<mx:Text x="80" y="20" id="headline10" text="" color="white"/>
<mx:Text x="80" y="40" id="price10" text="" color="white"/>
<mx:Text x="80" y="60" id="review10" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image11" click="naviToURL('11')"/>
<mx:Label x="80" y="0" id="label11" text="" color="white"/>
<mx:Text x="80" y="20" id="headline11" text="" color="white"/>
<mx:Text x="80" y="40" id="price11" text="" color="white"/>
<mx:Text x="80" y="60" id="review11" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image12" click="naviToURL('12')"/>
<mx:Label x="80" y="0" id="label12" text="" color="white"/>
<mx:Text x="80" y="20" id="headline12" text="" color="white"/>
<mx:Text x="80" y="40" id="price12" text="" color="white"/>
<mx:Text x="80" y="60" id="review12" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image13" click="naviToURL('13')"/>
<mx:Label x="80" y="0" id="label13" text="" color="white"/>
<mx:Text x="80" y="20" id="headline13" text="" color="white"/>
<mx:Text x="80" y="40" id="price13" text="" color="white"/>
<mx:Text x="80" y="60" id="review13" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image14" click="naviToURL('14')"/>
<mx:Label x="80" y="0" id="label14" text="" color="white"/>
<mx:Text x="80" y="20" id="headline14" text="" color="white"/>
<mx:Text x="80" y="40" id="price14" text="" color="white"/>
<mx:Text x="80" y="60" id="review14" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
<mx:HBox>
<mx:Canvas>
<mx:Image id="image15" click="naviToURL('15')"/>
<mx:Label x="80" y="0" id="label15" text="" color="white"/>
<mx:Text x="80" y="20" id="headline15" text="" color="white"/>
<mx:Text x="80" y="40" id="price15" text="" color="white"/>
<mx:Text x="80" y="60" id="review15" text="" color="white"/>
</mx:Canvas>
</mx:HBox>
<mx:HRule width="500"/>
</mx:VBox>
</mx:Application>