Tuesday, February 8, 2011

Sample XML Reader in Flex4


This is the Flex4 sample code.


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx"
               minWidth="955"
               minHeight="600">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
        <s:HTTPService id="imageData"
                       url="com/sampledata/images.xml"
                       resultFormat="e4x"
                       result="resultHandler(event);"
                       fault="faultHandler(event)"/>
    </fx:Declarations>
   
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.rpc.http.HTTPService;
           
            private var xmldata:XML;
           
            private function init():void
            {
                imageData.send();
            }
           
            private function resultHandler(event:ResultEvent):void
            {
                xmldata=event.result as XML;
                //Alert.show("resultHandler : " + xmldata);
                for each (var thexml:XML in xmldata.img)
                {
                    Alert.show("resultHandler : "+thexml.src);
                    //imageSourceArray.push(thexml.src);
                }
            }
           
            private function faultHandler(event:FaultEvent):void
            {
                Alert.show("In Fault");
            }
           
        ]]>
    </fx:Script>
    <s:Button x="161" y="117" label="Button" click="init()"/>
</s:Application>

Sample XML file.

<?xml version="1.0" encoding="utf-8"?>
<gallery>
<img>
<src>com/assets/images/slideShow/1.JPG
</src>
<caption>What a pretty flower</caption>
</img>

<img>
<src>com/assets/images/slideShow/2.JPG
</src>
<caption>This is the grand canyon</caption>
</img>

<img>
<src>com/assets/images/slideShow/3.JPG
</src>
<caption>bean!</caption>
</img>

<img>
<src>com/assets/images/slideShow/4.JPG
</src>
<caption>another image caption</caption>
</img>

</gallery>


 Folder Structure
























No comments:

Post a Comment