Learn how to load multiple XML designs with Flash (61) flash every day

Source: Internet
Author: User
Tags id3

To learn how to load an XML file in flash, it is very easy. You can only load the file by using urlloader. At present, there are many popular loading methods on the Internet, including a variety of open-source projects. You can easily process and manage external resources. However, it is convenient and efficient to use such powerful tools. But what should we do if there are no such tools? It is better to try to write one, although the function is not powerful, but at least we can not limit these tool libraries. This will make our development better.

 

Now let's write a program to load multiple XML files.

Production method:

 

Public class xmlmanager extends eventdispatcher

 

Inherit from eventdispatcher event dispatch class

 

Public Function add (Path: String, ID: string): void; // Add an XML file

 

Public Function start (): void; // start loading

 

Public Function getxml (ID: string): XML; // return the XML file

 

This combination method draws on these open-source class libraries.

 

 

Class inheritance event assignment

Add File

Start loading

The function of associating array dictionary is used to store objects.

 

TIPS:

Class item <br/>{< br/> Public var Loader: urlloader; <br/> Public var path: string; <br/> Public Function item (Loader: urlloader, path: string) <br/>{< br/> This. loader = loader; <br/> This. path = path; </P> <p >}< br/>}</P> <p>

 

Some data can be recorded in this class. This design can be called a class struct, just like a C ++ struct. This type facilitates data storage.

 

Use of correlated Arrays:

Public Function add (Path: String, ID: string): void <br/>{< br/> var Loader: urlloader = new urlloader (); <br/> m_data [ID] = new item (loader, PATH); <br/> num ++; <br/>}</P> <p>

In this way, an object can be saved like an array for convenient management.

 

 

 

Code List:

 

 Package <br/> {<br/> Import flash. events. *; <br/> Import flash.net. *; <br/> Import flash. utils. dictionary; <br/> public class xmlmanager extends eventdispatcher <br/> {<br/> private var m_data: dictionary = New Dictionary (true); <br/> private var num: int = 0; <br/> Public Function xmlmanager () <br/>{< br/>}< br/> // Add a file <br/> Public Function add (Path: String, ID: string ): void <br/> {<br/> var Loader: urlloader = new urlloader (); <br/> m_data [ID] = new item (loader, PATH ); <br/> num ++; <br/>}< br/> // start loading <br/> Public Function start (): void <br/> {<br/> for (var I: String in m_data) <br/> {<br/> m_data [I]. loader. load (New URLRequest (m_data [I]. path); <br/> m_data [I]. loader. addeventlistener (event. complete, completehandler); <br/> m_data [I]. loader. addeventlistener (ioerrorevent. io_error, errorhandler); <br/> m_data [I]. loader. addeventlistener (progressevent. progress, progresshandler); <br/>}< br/> // return XML data during loading <br/> Public Function getxml (ID: string): XML <br/>{< br/> var XML: xml = XML (m_data [ID]. loader. data); <br/> return XML; <br/>}< br/> // complete processing <br/> private function completehandler (Event: Event ): void <br/>{< br/> event. currenttarget. removeeventlistener (event. complete, completehandler); <br/> num --; <br/> If (num = 0) <br/>{< br/> This. dispatchevent (new event ("loadcomplete"); <br/>}< br/> // error handling <br/> private function errorhandler (event: ioerrorevent): void <br/>{< br/> This. dispatchevent (new event ("loaderror"); <br/>}< br/> // progress <br/> private function progresshandler (Event: progressevent ): void <br/>{< br/> This. dispatchevent (new event ("loadprogress"); <br/>}</P> <p> Import flash.net. urlloader; </P> <p> class item <br/>{< br/> Public var Loader: urlloader; <br/> Public var path: string; <br/> Public Function item (Loader: urlloader, path: string) <br/>{< br/> This. loader = loader; <br/> This. path = path; </P> <p >}< br/>}

 

 

Test the result of your class:

VaR XML: xmlmanager = new xmlmanager (); <br/> XML. add ("1.xml"," id1 "); <br/> XML. add ("2.xml"," ID2 "); <br/> XML. add ("3.xml"," ID3 "); <br/> XML. start (); <br/> XML. addeventlistener ("loadcomplete", completehandler); </P> <p> function completehandler (Event: Event): void <br/>{< br/> trace (XML. getxml ("id1"); <br/> trace (XML. getxml ("ID2"); </P> <p> trace (XML. getxml ("ID3"); <br/>}

 

 

Expansion:

In this way, we have achieved the purpose of loading without many requirements. This design of loading multiple XML files has basically been implemented. Now let's think about it.

If we load multiple MP3 files, how should we design them? Similarly, we can use this loading method to design such function classes. Combined with this, we can achieve a larger and more powerful load management class. Just like the bulkloader class and queueloader class in open-source projects. A little bit of implementation.

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.