Link to home
Start Free TrialLog in
Avatar of VeyronSA
VeyronSA

asked on

Flex Reading Image (that is linked like a banner) from XML to show in Application

Hi,

How can i show a Image with the url linked (like a banner) using XML and mx.Image? I would like to setup banners in my application and get the banner details from the XML file.

Thanks
AJ
Avatar of astaz3l
astaz3l

Avatar of VeyronSA

ASKER

Is there any way to get the data from the XML file and show it in mx:Image (not like an slideshow, static images :) ).

Thanks, total noob here
VeyronSA
Here is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<on_air>
  <dj>
    <name>Urban Borries</name>
    <show>Vivendi Universal</show>
    <img>http://www.domain.com/1.png</img>
    <blog>http://www.facebook.com</blog>
    <email>person@domain.com</email>
  </dj>
</on_air>

Open in new window


What i need is a way to show the details in the XML from my server to the desktop application.

Please show a sample that i can follow :)
SOLUTION
Avatar of petiex
petiex
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Petiex,

Thanks for the code :D

However i have a Error:

Description      Resource      Path      Location      Type
1008: variable 'pics' has no type declaration.      audioPlayer.mxml      /Desktop Audio Player/src/com      line 59      Flex Problem

Any idea?

Thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks :D
Just a quick question, is there a way that the application can refresh the data every hour or so?
Yes. You could set a timer to call loadXML() at whatever interval.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" percentHeight="100" percentWidth="100"
  creationComplete="setTimer()"
        >
    <mx:Script><![CDATA[

        private var myXML:XML;
        //You will want to make this much larger. I have it at 20 seconds
        //for testing.
        private const intervalInMilliseconds:Number = 20000;
        private function setTimer():void{
            loadXML(null);
            var timer:Timer = new Timer(intervalInMilliseconds,0);
            timer.addEventListener(TimerEvent.TIMER, loadXML);
            timer.start();
        }
        private function loadXML(event:Event):void {
            //add new date querystring to avoid browser caching
            var loader:URLLoader = new URLLoader(new URLRequest("http://localhost:9080/test.xml?v="+(new Date()).getTime()));
            loader.addEventListener(Event.COMPLETE,  loadComplete);
        }
        private function loadComplete(event:Event):void{
            myXML = new XML(event.target.data);
            myImage.source = myXML.dj.img;
            myImage.addEventListener(MouseEvent.CLICK, openLink);
        }
        private function openLink(event:MouseEvent):void{
            navigateToURL(new URLRequest(myXML.dj.blog))
        }
        ]]></mx:Script>
    <mx:Image id="myImage" useHandCursor="true" buttonMode="true"/>

</mx:Application>

Open in new window

By the way, I posted a possible answer for another of your questions last week, if you would like to have a look:
 https://www.experts-exchange.com/questions/26630815/Flash-Flex-change-xml-data-according-to-time.html
Hi Petiex,

Just 1 more question, i want to add another link for the email when clicking on a icon, is there a way that the script can see the difference between the blog and email link?

Thanks from a super Noob :D
If you add an mx:Image with id="emailIcon" in the mxml, then you can make it an email link by modifying your code like this (lines commented with //+ indicate added code):

        private function loadComplete(event:Event):void{
            myXML = new XML(event.target.data);
            myImage.source = myXML.dj.img;
            myImage.addEventListener(MouseEvent.CLICK, openLink);
            emailIcon.addEventListener(MouseEvent.CLICK, openEmail); //+
        }
        private function openLink(event:MouseEvent):void{
            navigateToURL(new URLRequest(myXML.dj.blog));
        }
        private function openEmail(event:MouseEvent):void{  //+
            navigateToURL(new URLRequest("mailto:"+myXML.dj.email)); // +
        }                                                                                   // +