Widget resources, widgets for download, tutorials and more at widgipedia.com
Popular Tags:

action adventure alfanet apple arcade blog blogs business cars clock dashboard drive flash free fun game games garageband google idvd ilife imovie iphoto islam iweb kpop mac macwidgets mario music news platform play puzzle search searchfree shoot sports strategy thedashboard tvwidgets websearch widget widgetindex widgettv
Log in to Widgipedia
The Widgipedia Forum : Widget Development
Discussions about widget technologies, platforms, news, ideas - the place that can shape the future of the widgets.
Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
platinum2151
(IP Logged)
February 05, 2007 06:46PM

messageUpdating widget from external XML file by date
I have a widget that I would like to have update daily by switching to the next element in an XML file triggered by the date. I have data fields set so that when the widget changes to the next XML element, it will write the text correctly, but I am having trouble with the images updating. I would like to have the image change depending on the image in the xml as well but dont know how to write this. Here is the code I have so far: (i wont bother including all the text fields and image tags)

<?xml version="1.0" encoding="UTF-8"?>

<action trigger="onTimer" interval="0.5">
var refresh = 5;
intcounter += 1;
if (intcounter == refresh)
{
update();
}
else
{
if (intcounter > refresh)
{
intcounter = 0;
}
}
</action>

<action trigger="onLoad">
main_window.visible = 1;
var intcounter = 0;
var lastTime = 0;

function update() {
var months = new Array("Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec.");

var date = new Date();
var day = date.getDate();
var month = date.getMonth();
var yy = date.getYear();
var year = yy + 1900;

var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = 'am';

if (minutes < 10)
minutes = "0" + minutes;

if (seconds < 10)
seconds = "0" + seconds;

if (hours >= 12)
{
var ampm = 'pm';
}

if ( (hours > 12) )
{
hours -= 12;
}

datestring.data = string = months[month] + " " + day + ", " + year;

timestring.data = string = hours + ":" + minutes +ampm + " CT";

var doc = XMLDOM.parse(filesystem.readFile("test.xml"));

first_team.data = doc.evaluate("string(games/game1/first_team)");

second_team.data = doc.evaluate("string(games/game1/second_team)");

time_location.data = string = "Next Game: " + doc.evaluate("string(games/game1/date)") + " @ " + doc.evaluate("string(games/game1/time)");

notes.data = doc.evaluate("string(games/game1/notes)") + ", " + doc.evaluate("string(games/game1/location)");

}

update();

</action>

</widget>

The time and date code work great, and so do the data fields, here are my two problems.
1. I would like the images to change like the text will, i just dont know the syntax.
2. How can I make the widget call a new XML node everyday drived by the date? Like have it get Feb.1st's node on Feb. 1st?

Any help or ideas at all would be helpful. I pretty new at all this and have come a long way to not be able to finish, thanks alot.

  
kaintze
(IP Logged)
February 06, 2007 03:11AM

messageRe: Updating widget from external XML file by date
If you have the full URL in the XML file, all you need to do is set the image's src to that:

yourimageobject.src = doc.evaluate("string(games/game1/imageURL)");


If your XML contains a part of the URL, you might need to compose it, like:

yourimageobject.src = 'http://yourdomain.com/' + doc.evaluate("string(games/game1/imageURL)");


Accessing a new XML node based on the date really depends on how your XML looks like. You might need to select a node based on an attribute. If you post your XML here, I can give it a look.

  
platinum2151
(IP Logged)
February 07, 2007 08:54AM

messageRe: Updating widget from external XML file by date
This is what I have for the XML. What's in here now is just test code to try to make the widget come get it all. I will need to add more such as "game2, game3...etc." or whatevr I need to name them for each game. This is what the daily update needs to do, go from one game to another to change the information so that it is correct for the upcoming game.

<?xml version="1.0" encoding="UTF-8"?>

<games>

<game1>
<date>4/3/2006</date>
<time>3:05 PM</time>
<first_team>Cardinals</first_team>
<second_team>Phillies</second_team>
<location>Citizens Bank Park</location>
<notes>Phillies home opener</notes>
</game1>

</games>

  
platinum2151
(IP Logged)
February 07, 2007 08:55AM

messageRe: Updating widget from external XML file by date
Forgot to say thanks for the help! Your the only person in multiple forums who has been willing to even respond.

  
kaintze
(IP Logged)
February 07, 2007 02:41PM

messageRe: Updating widget from external XML file by date
For the particular case of this XML, you need to select the child node of the <games> node that has the <date> field equal to the current date. The XPath syntax is a bit tricky, but thumb rule is you replace "games/game1" in your original code with "games/*[date='" + mdy + "']" (meaning select those child nodes from games that have the date equal to mdy, a variable you built from the month, day and year):

// Build the date to compare with the XML date
var mdy = month + '/' + day + '/' + year;

first_team.data = doc.evaluate("string(games/*[date='" + mdy + "']/first_team)");
second_team.data = doc.evaluate("string(games/*[date='" + mdy + "']/second_team)");
...


Seeya!

  
platinum2151
(IP Logged)
February 07, 2007 05:14PM

messageRe: Updating widget from external XML file by date
I didn;t know you could call a variable like that inside the XPath. That will be the best way to do it. When I put the below code in though I get no output, no text.

var mdy = month + "/" + day + "/" + year;

var doc = XMLDOM.parse(filesystem.readFile("schedule.xml", false));

first_team.data = doc.evaluate("string(games/*[date='" + mdy + "']/first_team)");


Am I missing a step? I think I can set this to be like "=" or "<" so that f there is ano off day, meaning no node woth the equal date, it will pick up the next game the day after. Thanks again for the help, your idea makes it way better than writing a seperate update .js file like I thought I was gonna have to do.

  
platinum2151
(IP Logged)
February 07, 2007 06:26PM

messageRe: Updating widget from external XML file by date
Got the glitch figured out, my "var month = date.getMonth(); is pulling the right data, but it's starting the monthes on 0 not 1. So my game data is pulling one month behind. Not sure how to fix that either.

  
platinum2151
(IP Logged)
February 07, 2007 06:47PM

messageRe: Updating widget from external XML file by date
var month = date.getMonth() + 1;

Simple as that. Now just to figure out the off day glitch i mentioned above and this will finally done.

  


Sorry, only registered users may post in this forum.
Check out our sister site Dealio for Cyber Monday Deals and Nordstrom Coupons.
This forum powered by Phorum.