Solved

Flash/Flex change xml data according to time

Posted on 2010-11-22
5
498 Views
Last Modified: 2012-05-10
Hi All,

Busy with a project, but need help to compile a flash/flex component to change data got from XML according to time.

Here is my XML sample:
<?xml version="1.0"?>
<catalog>
   <dj id="00h00>
      <onair>Gambardella, Matthew</onair>
      <showname>Grave Shift</showname>
      <image>http://www.domain.com/app/images/matthew.png</image>
      <blog>http://www.domain.com/blog/mattheew</blog>
      <email>matthew@domain.com</email>
   </dj>
   <dj id="03h00>
      <onair>Delport, Mardi</onair>
      <showname>After Night</showname>
      <image>http://www.domain.com/app/images/mardi.png</image>
      <blog>http://www.domain.com/blog/mardi</blog>
      <email>mardi@domain.com</email>
   </dj>
</catalog>

Open in new window


What i need is a way to display the details according to different times (radio station). When on certain time, the component change the data gotten from XML document.

Please help :)
0
Comment
Question by:VeyronSA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:petiex
ID: 34190180
You can build a string like the ones you have in the dj id value from the current date object, and then, since alphabetical order and time order are the same in this case, the first dj time slot that is in the past will be the active one:
private function chooseDJ(x:XML):XML{
            var date:Date = new Date();
            var nowH:int = date.getHours();
            var nowM:int = date.getMinutes();
            var activeDj:XML = null;
            var nowStr:String = (nowH > 9?"":"0")+nowH+"h"+(nowM > 9?"":"0")+nowM;
            for each (var dj:XML in x.dj){
                if(dj.id < nowStr){
                    activeDj = dj;
                    break;
                }
            }
            return activeDj;
        }

Open in new window

0
 
LVL 11

Expert Comment

by:petiex
ID: 34190247
Then, of course, if the current time is earlier than the earliest time slot, you cycle through the dj xmls to get the latest timeslot...


private function chooseDJ(x:XML):XML{
            var date:Date = new Date();
            var nowH:int = date.getHours();
            var nowM:int = date.getMinutes();
            var activeDj:XML = null;
            var latestSlot:XML = null;
            var nowStr:String = (nowH > 9?"":"0")+nowH+"h"+(nowM > 9?"":"0")+nowM;
            for each (var dj:XML in x.dj){
                if(!latestSlot || latestSlot.id < dj.id){
                    latestSlot = dj;
                }
                if(dj.id < nowStr){
                    activeDj = dj;
                    break;
                }
            }
            if(activeDj == null){
               activeDj = latestSlot;
             }
            return activeDj;
        }

Open in new window

0
 

Author Comment

by:VeyronSA
ID: 34233868
Ok, lol. Now i dont know how to integrate :D. A big beginner and a Newbie with this, can you show me please.
0
 
LVL 11

Accepted Solution

by:
petiex earned 500 total points
ID: 34234297
I'll assume this is an extension of your other question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Flex/Q_26581471.html) and plug the new code into that. I'm glad I tested this again, because the code I posted above doesn't actually work :-O

The attached code holds up to testing -- and the timer makes it easy to test out different values in the xml without redeploying.
<?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://www.domain.com/test.xml?v=" + (new Date()).getTime()));
            loader.addEventListener(Event.COMPLETE, loadComplete);
        }

        private function loadComplete(event:Event):void {
            myXML = chooseDJ(new XML(event.target.data));
            myImage.source = myXML.image;
            myImage.addEventListener(MouseEvent.CLICK, openLink);
        }

        private function openLink(event:MouseEvent):void {
            navigateToURL(new URLRequest(myXML.blog))
        }

        private function chooseDJ(x:XML):XML {
            var date:Date = new Date();
            var nowH:int = date.getHours();
            var nowM:int = date.getMinutes();
            var activeDj:XML = null;
            var latestSlot:XML = null;
            var nowStr:String = (nowH > 9 ? "" : "0") + nowH + "h" + (nowM > 9 ? "" : "0") + nowM;

            for each (var dj:XML in x.dj) {
                if (!latestSlot || latestSlot.@id < dj.@id) {
                    latestSlot = dj;
                }
                if (dj.@id < nowStr) {
                    if (!activeDj || activeDj.@id < dj.@id) {
                        activeDj = dj;
                    }
                }
            }
            if (activeDj == null) {
                activeDj = latestSlot;
            }

            return activeDj;
        }
        ]]></mx:Script>
    <mx:Image id="myImage" useHandCursor="true" buttonMode="true"/>

</mx:Application>

Open in new window

0
 

Author Closing Comment

by:VeyronSA
ID: 34234551
Genius!!!! Give this man a Medal!

Thanks
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert Oracle data into XML document 2 80
TSQL Query Into Specific XML Format 3 45
xml files 7 63
splitting a sting into elements 6 72
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question