MXMLTest06 External XML Read Test (外部XMLから画像URLを取り出して画像表示)

by siouxcitizen forked from MXMLTest05 External XML Read Test (取得した外部XMLから指定したデータを取り出す) (diff: 103)
♥0 | Line 107 | Modified 2009-04-19 07:48:17 | MIT License
play

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/eDmv
 */

<?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)を処理
            //取得した外部XMLから画像URLを取り出して画像を表示
            //keiso's Flickr pipesのように華麗にループで画像表示も、リンク貼り付けも、
            //と思ったけどできない。。。  
            //結局、リンク貼り付けはできず、画像表示のみ。。。
            //ベタベタなイメージコンポーネントにベタベタSwitch構文で画像表示。。。
            private var query:String = "出石そば";//検索キーワード 
            private var URL:String = "http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch?appid=uQ_E1kmxg64.0fo1fkMwTnvcEmBn.foaHDXp0MF8WvUXD3jQcDaDIAuC0b6zRFmh3BT2umN6&query=" + escapeMultiByte(query); 
            private var loader:URLLoader;
            private var xmlData:XML;
            private var ns:Namespace = new Namespace("urn:yahoo:jp:itemSearch"); 
            
            private function testIamgeDisplay():void {
                //外部XML処理中はボタンを不活性化
                btnTest.enabled = false; 
                
                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.");
                }    
            }
            
            private function completeHandler(event:Event):void {
                xmlData = new XML(loader.data);
                var count:uint = 0;
                default xml namespace = ns; //名前空間設定
                //取得した外部XMLから画像URLを取り出して画像を表示           
                for each(var element:Object in xmlData.Result.Hit){
                    var imgURL:String = element.Image.Small;
                    ctrlDispInfo(count, imgURL);
                    count++;
                }
                btnTest.enabled = true; //外部XML処理後にボタンを活性化           
            }
            
            private function ctrlDispInfo(count:uint, imgURL:String):void{
                switch (count) {
                case 0:
                    image00.source = imgURL;
                    break;    
                case 1:
                    image01.source = imgURL;
                    break; 
                case 2:
                    image02.source = imgURL;
                    break; 
                case 3:
                    image03.source = imgURL;
                    break; 
                case 4:
                    image04.source = imgURL;
                    break; 
                case 5:
                    image05.source = imgURL;
                    break; 
                case 6:
                    image06.source = imgURL;
                    break; 
                case 7:
                    image07.source = imgURL;
                    break; 
                case 8:
                    image08.source = imgURL;
                    break; 
                case 9:
                    image09.source = imgURL;
                    break; 
                case 10:
                    image10.source = imgURL;
                    break; 
                case 11:
                    image11.source = imgURL;
                    break; 
                case 12:
                    image12.source = imgURL;
                    break; 
                case 13:
                    image13.source = imgURL;
                    break;  
                case 14:
                    image14.source = imgURL;
                    break; 
                case 15:
                    image15.source = imgURL;
                    break;   
                default:
                    break;           
                }
            }           
        ]]>
    </mx:Script>
    <mx:Button id="btnTest" click="testIamgeDisplay();" x="10" y="10" label="ボタン" enabled="true"/>
    
    <mx:Image id="image00" x="10" y="50"/>
    <mx:Image id="image01" x="110" y="50"/>
    <mx:Image id="image02" x="210" y="50"/>
    <mx:Image id="image03" x="310" y="50"/>
    
    <mx:Image id="image04" x="10" y="150"/>    
    <mx:Image id="image05" x="110" y="150"/>
    <mx:Image id="image06" x="210" y="150"/>
    <mx:Image id="image07" x="310" y="150"/>
    
    <mx:Image id="image08" x="10" y="250"/>    
    <mx:Image id="image09" x="110" y="250"/>
    <mx:Image id="image10" x="210" y="250"/>
    <mx:Image id="image11" x="310" y="250"/>
    
    <mx:Image id="image12" x="10" y="350"/>    
    <mx:Image id="image13" x="110" y="350"/>
    <mx:Image id="image14" x="210" y="350"/>
    <mx:Image id="image15" x="310" y="350"/> 
        
</mx:Application>

Forked