|
|
||||
|
|
Ultrashock Tutorials > Flash5 > XML and Flash5 | |||
|
||||
|
|
XML and Flash5 |
|
||
|
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? Click here to see a demo of the Flash5 FLA in the accompanying zip file. 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. 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 Common Data Structure Information Systems 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. 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: 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. 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. Due to the pitfalls of the Flash XML parser, the other alternatives may be the only option for loading in information to Flash. 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 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. 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. 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 2. White spaces in your XML 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 4. Flash specific XML 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:
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. 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. |
||||
|
©2001 Ultrashock.com
Inc. - All rights reserved
|