| The
first thing that needs to be done is
extract the XML data. This is performed
in the first frame of the Text layer.
And is displayed below:
//
Declare Catalog XML object, and load
data file
stop();
//Initialise the hitURL
hitURL = "http://www.tupps.com";
newsXML = new XML();
newsXML.onLoad = ExtractData;
newsXML.load("news.xml");
gotoAndPlay(2)
//
ExtractData Function
function ExtractData(success) {
_root.newsItems = new Array();
//Check if our XML file has been successfully loaded
if (success) {
for (counterA in this.childNodes)
{
for (counterB
in this.childNodes[counterA].childNodes) {
if
(this.childNodes[counterA].childNodes[counterB].nodeName == "newsitem")
{
//Creates
a new NewsItem object (see function at bottom of script)
myNewsItem
= new NewsItem(
this.childNodes[counterA].childNodes[counterB].firstChild.nodeValue,
this.childNodes[counterA].childNodes[counterB].attributes.link);
//Spans 3 lines
//Assign
myNewsItem to the end of the array, of news items.
_root.newsItems.push(myNewsItem);
trace("_root.newsItems.length: " +
_root.newsItems.length);
}
}
}
//end of if statement
} else {
//Create a hard coded link just
in case the XML has failed to load!
myNewsItem = new NewsItem(
"Welcome
to Tupps.com", "http://www.tupps.com"); //Spans 2 lines
//Assign myNewsItem to the end
of the array, of news items.
_root.newsItems[_parent.newsItems]
= myNewsItem;
}
}
function
NewsItem(DisplayName, URL) {
this.DisplayName = DisplayName;
this.URL = URL;
}
Going
from the top, the hitURL variable is
used by the HitButton layer to take
the user to another site. It is initialized
here so that if the user clicks the
latest news before the XML is loaded
they will be taken to a web site rather
than being shown a blank page.
The next three lines of code creates
a new XML container object, newsXML.
The second line calls ExtractData when
XML data has been loaded. The third line
loads the news.xml file. The link to
the news.xml file does not specify a
path so it will always look in the same
directory that the swf has been loaded
from.
newsXML
= new XML();
newsXML.onLoad = ExtractData;
newsXML.load("news.xml");
The
final bit of code in the main function
is to set the flash player to play frame
2. Frames 2 and 3 are set to loop until
the XML has been loaded.
The
ExtractData function starts by creating
an array for NewsItem objects which will
contain the news headlines and the links.
The contstructor for the NewsItem object
is at the very end of the code. The array
is created on the _root time line to
make it accessible globally. The success
parameter is checked to make sure that
the XML has been loaded successfully.
If the XML was not loaded successful
then some default text and links can
be used instead.
Next
the data from within the XML needs to
be extracted. One problem with Flash's
XML parser is that it includes whitespace
(carriage returns, tabs etc) as XML nodes.
To avoid any problems with whitespace
parse all the XML nodes is is important
to look specifically for the node that
is required. Doing this will also allow
our application to continue to work if
other fields are added to the XML file.
We
want to get all of the 'newsitem' elements.
Two nested for loops are used to check
all the elements.
for (counterA in
newsItemsXML) {
for (counterB in newsItemsXML[counterA].childNodes)
{
The
first for loop will select the <news> node.
The second for loop looks at all the
nodes in the <news> nodes. We check
to make sure that it is a newsitem node,
a check is made to ensure that is not
whitespace:
if (newsItemsXML[counterA].childNodes[counterB].nodeName
== "newsitem") {
Once
the newsItem node is identified we
can extract the data:
_root.newsItems[_root.newsItems.length]
= newsItemsXML[counterA].childNodes[counterB].firstChild.nodeValue;
_root.newsLinks[_root.newsLinks.length]
= newsItemsXML[counterA].childNodes[counterB].attributes.link;
The
first line reads the information contained
between start and end tags. For the
first line of our XML file this would
extract "Flash Section Created".
The second line reads the attribute
contained within the start tag. In
our case it would grab "http://www.tupps.com/flash/index.html" from
the XML file.
|