Flex reads external XML files

Source: Internet
Author: User

Because Flex supports powerful E4X functions, xml file operations In flex are very simple. Below, we will summarize the common methods for reading XML configuration files:

1. Use the Model label format

First declare the Model Tag:

<Mx: Model id = "danxuan" source = "myData/danxuan. xml"/>

<Mx: DataGrid dataProvider = "{danxuan. ti}">

</Mx: DataGrid>

2. Use XML tags

<Mx: XML id = "danxuan" source = "myData/danxuan. xml"/>

<Mx: DataGrid dataProvider = "{danxuan. ti}">

 

3. Use HttpService

<Mx: HTTPService id = "danxuan" url = "myData/danxuan. xml"

Result = "resultHandler (event)"/>

Private function resultHandler (event: ResultEvent): void

{
Var dp: ArrayCollection = event. result as ArrayCollection;
}

 

At the same time, add danxuan. send (); To the createComplete event of the tag Application, and call the Http request (where "danxuan" is the id of mx: HTTPService.

That is: <mx: Application xmlns: mx = "http://www.adobe.com/2006/mxml" createComplete = "danxuan. send ();">

 

4. Use URLLoader

Sometimes we want to use xml as the configuration file to dynamically configure our system. Although flexSDK provides a tag that can only be used in the mxml file <xml/> and <model/>, the source attribute of these tags can read external xml files, however, this method will embed the xml file into the generated swf file. The result is that after the release, even if the xml content is modified, the program will not be affected. This does not enable external xml files. To dynamically read xml files, you need to use the URLLoader class. This class is located in the flash.net package.

1. Create a URLRequest object

To use URLLoader to read external xml, you must call the load () function of URLLoader. This function has a URLRequest parameter. Therefore, you must first create a URLRequest object.

 

Var url: URLRequest = new URLRequest ("myData/danxuan. xml ");

 

The URLRequest parameter must be a valid url.

Ii. Read external xml

1. Create a URLLoader object.

Var loader: URLLoader = new URLLoader ();

 

 

2. Register Event listening for the newly created object. We need to listen to the Event. COMPLETE Event, which means that the xml file is read.

 

Loader. addEventListener (Event. COMPLETE, onComplete );

 

OnComplete is the processing function of Event. COMPLETE Event. This function is called when Event. COMPLETE Event occurs.

 

3. Call load function count

Loader. load (url );

 

 

The url parameter is the previously created URLRequest object.

Iii. event processing function onComplete

 

The complete definition of the function looks like this:

Private function onComplete (event: Event): void {}

 

 

This function has been registered to the Event. COMPLETE Event of the URLLoader object. When Event. COMPLETE Event occurs, this function will be called. The next question is how to get the xml data.

First, convert the target attribute of the onComplete function parameter event to the URLLoader object.

 

Var result: URLLoader = URLLoader(event.tar get );

 

Then convert the data property of this object into an xml object.

 

Datas = XML (result. data );

 

In this way, the content of danxuan. xml can be read to datas.

This method can be used to dynamically read xml files. You can dynamically configure programs to provide dynamic data sources. The specific application depends on your needs.

Var urlLoader: URLLoader = new URLLoader ();
UrlLoader. addEventListener (Event. COMPLETE, handleURLLoaderCompleted );
UrlLoader. load (new URLRequest ("myData/duanxuan. xml "));

Private function handleURLLoaderCompleted (event: Event): void

{
Var loader: URLLoader = event.tar get as URLLoader;
Xml = XML (loader. data );
}

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.