In 1995 we saw the release of FutureSplash, a program which we have come to know as Flash. Originally it was designed to bring lightweight animation to the Internet. Now in the hands of Macromedia, it has become a powerful tool enabling creators to make highly interactive and graphically rich websites. 1999's addition of Flash 5 saw many powerful improvements which allowed us to extend our Flash applications to create dynamic, functional websites. Possibly the most valuable feature of the new additions was its ability to work in conjunction with other tec 14414m1216o hnologies. In this tutorial I will look at integrating Flash with other technologies to create easily updatable websites specifically focussing on combining XML and Flash. I will also consider alternatives to XML including server-side scripting languages such as PHP, Perl or ASP.
Updating your Flash website can be a very easy process. It all revolves around loading your information into Flash on runtime. This new information then becomes available to Flash and you can use it to create user specific settings. There are many methods to update your Flash website without the use of Flash. This is a very powerful thing making your websites really easy to update. A few methods include loading information from .txt files, XML files and even information straight from a database with the help of a server-side scripting language.
NOTE: This article relates to XML in the context of it being used with Flash, not other web environments or platforms. XML in Flash is a little more limited than using XML in most other environments.
Sections covered are:
What is XML?
Why should we use XML?
Alternatives to XML?
Refining the use of XML?
Pitfalls with XML in Flash?
Data Structure?
Implementing XML?
Click here to see a demo of the Flash5 FLA in the accompanying zip file.
What is XML?
XML is a standard that has been developed to explicitly deal with the description and structuring of data. It has been designed to help you create your own data structure. XML is used as more of an aid in creating your own data structure rather than a language such as PHP, c++, etc.
Why and when to use XML?
You may ask why should we use XML when HTML is already being used on the web and for exchanging data. Well XML and HTML actually do very different things. A HTML file contains tags which define how a HTML page, when rendered, will be displayed. An XML file contains customs tags for organizing information. We use these custom tags to create our own specific data structure. A common data structure should be designed that will cater for all necessary information.
Deciding when to use XML can be a difficult question to answer. XML is platform and language independent, which means you can use XML on any platform from a Microsoft OS, to a Linux based OS to an Apple based OS. This means XML is great for exchanging data between platforms.
Following are a couple of scenarios where XML may be a good choice:
Platform
and Location Independent Data Exchange
Web based applications can use XML to transfer data between users computers in
a company where there is a need to have many different operating platforms.
Using the web as the storage location also makes this information location
independent giving the users powerful access to the same information regardless
of platform or location. This is very powerful for companies with clients
across the world where a lot of traveling is necessary.
Common
Data Structure Information Systems
Creating a common data structure for use with frequently accessed/updated
information can greatly enhance interactivity with other companies, business,
and individuals. All that these businesses, companies and individuals need to
interact and take advantage of this information is knowledge of the data
structure. They can then transfer this XML information into any platform or
application they wish.
Despite the above, XML is not necessary or the best option for bringing data into Flash. Depending on the requirements of the application there may be a faster, more efficient way to achieve this.
There are a few points I keep in mind when deciding if I should use XML:
1. If there is no real need to have the information in an XML format, then don't put it into XML just for use with Flash. If it is just as easy to use an alternative method then go for that option.
2. Use XML where the information needs to be platform/application independent. So if you are using the information in a variety of different ways, XML may be a good choice. Also think about future developments and possible applications, as having your data in an XML format will make it easy to adapt your XML into a format that is useful to your needs.
3. Try to limit using XML to data that is being updated regularly, or needs to be changed quickly and easily. One of the powers of XML is that is very readable by humans and easy to understand.
There are of course going to be other scenarios where XML is a good choice, but I use these points as a general guide when working with Flash.
Alternatives to XML?
There are a few alternatives to XML, which can be used to get information into Flash. Sometimes these alternatives are a much better option allowing you more room to breath and adapt your application. Explore your options to make sure that you are using the best method available to you. You may find a different method that will provide you with more stability and speed to your application.
Some
alternatives:
1. Loading in .txt files residing on your server
2.Passing Flash a query string (name=value&name1=value1) containing new
information
3.Use a server side scripting language to retrieve information directly from a
database
4.Using HTML and JavaScript to pass information to Flash
You
need to examine and decide if one of the alternatives is more suited to a
current application. Remember to evaluate things such as speed, reliability and
data availability.
For example if you are bringing in a large amount of information into Flash
such as five news pages that change daily, I would use a .txt file and load the
information in on runtime.
Using a .txt file would make this loading process faster then if you were
loading the information in with XML.
Flash
works with XML in a way that it needs to first load (download) the XML document
and then parse it before any of the data can be used within Flash. If you have
a large XML document this can take some time and slow up your application.
Loading the same information from a .txt file from the server will be faster as
once the data has loaded the information is immediately accessible.
Using XML documents that are created for other applications can be really
disastrous when trying to merge with Flash. The XML may have to be heavily
modified for use in your Flash application due to the Flash XML parser. The Flash
XML parser is really picky when it comes to parsing XML. It has a few issues,
which slow this process down dramatically. A few considerations need to be
taken when using Flash and XML.
Due to the pitfalls of the Flash XML parser, the other alternatives may be the only option for loading in information to Flash.
Refining the use of XML?
There are a few ways to use XML in your Flash application. Some are better than others, but you should always try to refine your method keeping things in mind such as:
1.
Database load
2. Server load
3. Number of Hits per day
4. The need for up to date information
5. Efficiency
Lets take the following example and then improve the application for speed, reliability and efficiency:
You are the content manager for an e-commerce provider. Your site has an up to date news section that you update every one to two days. To update your website you have an backend interface which adds the new data to the database. Then every time a user visits your page, Flash calls a PHP page. This PHP page reads the data from your database and returns it to Flash in a generic XML format.
You have a large number of daily visitors who visit the site primarily for the news section. On every hit to this news section Flash receives the data from the database and formats the XML ready for use. The XML is parsed to Flash in a generic format because you also offer the website in HTML format. The same PHP code is used to retrieve the data from the database for both Flash and HTML versions. The XML is then modified within Flash and displayed for the users to read. When Flash receives the XML data it must first remove white space from the XML data. This is due to Flash's XML parser, which does not like white space. If it finds any white space in your data it will turn it into and empty node, creating havoc within your XML tree.
We will now discuss some improvements that can be made to the example above. These improvements will speed up the loading process and make it more efficient reducing the load on Flash, the server and the database.
NOTE: For this example we will decide to stay with using XML just to illustrate the process. There may be better alternatives.
A
major improvement would be instead of calling a PHP page to serve the
information to Flash every time you get a hit, use PHP to create an .xml file
that resides on the server every time you update your database. This way you
reduce your database usage and possibly prevent a nasty bottleneck in your
system.
Reading a file off the server is a lot less work for the server then getting
information out of a database with PHP, especially when you have 3000 hits a
minute. It also uses only one resource being the server where as the current
method uses the server, PHP and the database.
Flash would then read in the XML file, which was created specifically for Flash, so it would have no white spaces. You may also consider using attributes rather than extra nodes. The more nodes you have in the XML data means the longer it will take Flash to parse the XML. It is much faster to do the enhancing before you load the data into Flash, as PHP will perform the same tasks much faster.
Your PHP code could easily be modified if you wanted to use the XML data elsewhere and needed another data structure. That is one of the advantages of having the data in a database, the output can be whatever you like and you don't have to have multiple versions of the same data.
These improvements would enhance this website reducing both server load and client side RAM usage increasing speed, efficiency and usability.
Pitfalls with XML in Flash
There are a few pitfalls that should be kept in mind when developing with Flash and XML.
1.
Parsing XML causes the flash player to halt
When parsing an XML document, the flash player pauses. Nothing else is being
performed at the time of parsing XML, so if you have a large amount of XML
information, your users will be waiting. This kind of defeats the purpose of
Flash being streaming media, as when it is parsing XML, it isn't streaming
anything.
2.
White spaces in your XML
The Flash XML parser cannot deal with white spaces in XML. If it finds any
tabs, line breaks, etc, it will turn these into empty nodes giving you a real
problem including a broken XML tree.
XML is used to create a data structure so your information can be easily
accessed. Adding empty nodes will make it very difficult to access the
information.
This problem occurs in version 5.0.41. However, this problem is fixed in version 5.0.42+ of the Flash Player with the addition of the ignoreWhitespace() function. Despite the improvements to the Flash player in version 5.0.42+, building an application to suit this player is very unwise unless it is being built for an intranet or something similar where Flash Player versions can be controlled. Most people do not know that more than one version of the Flash 5 Player exists. Most people presume there is only one, so you'll have trouble convincing people when they are presented with a screen informing them they do not have the latest version of the Flash Player.
I would suggest preparing your XML for Flash before loading and parsing it. This will greatly speed up the XML parsing process, as you wouldn't have to go through all the nodes in the XML and remove any empty ones. You can do this through server side scripting languages or remove white space from the .xml file before loading it to the server.
3.
Node Navigation
Node navigation can get a little tricky depending on the size of your XML data.
Using pointers like parentChild.parentChild.nodeName() can become a little
confusing and you can get lost easily. I prefer to load my XML data in and then
put the information into arrays while I am parsing the XML. This makes it a lot
easier to get access to the information later on.
4.
Flash specific XML
To get the best performance out of Flash and XML you really do need to create
Flash specific XML or at least modify the XML document to make it readable by
Flash. This in most cases will be a little time consuming and unique to each
set of data structures. This is one of the reasons that using one of the
alternatives to XML may be a better option, for speed, reliability and
efficiency.
Data Structure?
There are a few ways to structure your XML document and depending on how you do it, can speed up the parsing time. Using XML attributes is faster then having separate nodes for each bit of information. Lets take this example:
<month name="January">
<day>
<date>01</date>
<name>Thursday</name>
<time>12:03
PM</time>
<comment>It
is a hot day</comment>
</day>
<day>
<date>05</date>
<name>Monday</name>
<time>09:28
AM</time>
<comment>Another
hot day</comment>
</day>
</month>
The above data structure uses a new node for each bit of core information. It is well laid out and very readable by humans, however a better structure exists when using Flash. The data structure below makes more use of the attributes feature, speeding the parsing process up. It takes longer for Flash to read 6 separate nodes, than it does 6 attributes of a node. The data structure below is suited better for use with Flash.
<month name="January">
<day
date="01" name="Thursday" time="12:03 PM"
comment="It is a hot day" />
<day
date="05" name="Monday" time="09:28 AM" comment="Another
hot day" />
</month>
I have not outlined methods or ideas behind what data structures and formats should be used where, as this depends on the type of information and requirements in using the data. You should read up on XML and experiment with Flash and XML. Design your data structure around speed and efficiency tests which you should perform with each new data structure.
Implementing XML?
There are many ways to implement and start using XML in Flash. Things to keep in mind are loading time and XML data size. The larger your XML data means the longer Flash will take to parse it, which means the longer your users will wait.
If your data is coming straight from a database and you put it into XML before loading it into Flash, make sure your XML is well formed and clean, ready for Flash to start using the new information straight away.
Always make sure that XML is really necessary for your application; if it isn't then you should think about using an alternative to XML. XML however is a very good choice should your project need to be platform, application independent.
On the whole, the new XML parser is an added value to Flash but use it wisely. Think about the whole process and keep the pitfalls in mind. The simple method is usually the best choice. Scope you project and develop code that is extensible so should you need to develop your application further at a later date, it is an easy process.
|