As3 operation XML

Source: Internet
Author: User

Quick Start example:

Example 1. Read XML

1. var testxml: XML;
2. var file: file = file.doc umentsdirectory. resolvepath ("mousebomb/test. xml ");
3. var filestream: filestream = new filestream ();
4. filestream. Open (file, filemode. Read );
5. testxml = XML (filestream. readutfbytes (filestream. bytesavailable ));
6. filestream. Close ();

In this example, read the content using readutfbytes () and convert it to an XML object.

Example 2. write XML

1. var testxml: xml = <mousebomb> <site> www.mousebomb.org </site> <blog> www.flashj.cn </blog> </mousebomb>;
2. var file: file = file.doc umentsdirectory. resolvepath ("mousebomb/test. xml ");
3. var filestream: filestream = new filestream ();
4. filestream. Open (file, filemode. Write );
5. var outputstring: String = '<? XML version = "1.0" encoding = "UTF-8"?> /N ';
6. outputstring + = testxml. toxmlstring ();
7. filestream. writeutfbytes (outputstring );
8. filestream. Close ();

Writing XML is as simple as creating a file object and a filestream object, and writing data using writeutfbytes.
Workflow

To complete the file read and write operations, follow these steps:
1. Create a file object pointing to the file path
2. initialize the filestream object
3. Use the open () or openasync () method of filestream
4. If asynchronous openasync () method is used, you need to set event listening for filestream.
5. Add the required data read/write code
6. After the file operation is completed, execute the close () method of filestream.
Knowledge about using filestream

1. filemode
The open () and openasync () Methods of filestream both contain a filemode parameter, which is used to set:

* File Reading Capability
* File writing capability
* Always append data to the end of the file (when writing data)
* How to operate when the file does not exist (or when the file's parent level does not exist)

Specific values include

Filemode Value

Description

Filemode. Read

Set the file opening mode to read-only

Filemode. Write

Set the file opening mode to write data. If the file does not exist, it is created. If the file exists, all existing data of the file is deleted.

Filemode. append

Set the file opening mode to append. If the file does not exist, it is created. If the file exists, all existing data in the file is not overwritten, and all written data starts from the end of the file.

Filemode. Update

Set the file opening mode to read/write. If the file does not exist, create it. This mode is usually used to read and write files randomly. It can be read from any location of the file. When writing data, only the existing bytes at the write location are overwritten, and all other bytes are not affected.

2. Position
This attribute determines the location of the next data read/write operation.
Before reading and writing, set the position attribute to a valid position in the file, for example:

1. var myfile: file = file.doc umentsdirectory. resolvepath ("mousebomb/site.txt ");
2. var myfilestream: filestream = new filestream ();
3. myfilestream. Open (myfile, filemode. Update );
4. myfilestream. Position = 8;
5. myfilestream. writeutfbytes ("hello ");

In this example, the UTF-8 encoded string "hello" is written at location 8"

The position value of the newly opened filestream object is 0. Before reading a file, the position value must be at least 0 and smaller than the total number of bytes of the file.
The position value is changed only in the following situations:

1. directly set the attribute value
2. Perform read Operations
3. Perform write operations

When a read/write operation is performed, the value of position will immediately increase the number of read/write bytes. When a read/write operation is performed again, it will start from the new position:

1. var myfile: file = file.doc umentsdirectory. resolvepath ("mousebomb/test.txt ");
2. var myfilestream: filestream = new filestream ();
3. myfilestream. Open (myfile, filemode. Update );
4. myfilestream. Position = 4000;
5. Trace (myfilestream. position); // 4000
6. myfilestream. writebytes (mybytearray, 0,200 );
7. Trace (myfilestream. position); // 4200

There is one exception to position: if the file opening mode is set to append (append mode), the position attribute will not change with the write operation. In append mode, data is always written to the end of the file, regardless of position.

The file is opened asynchronously, And the write data operation is not completed when the next line of code is executed. What should we do? It doesn't matter. You can call multiple asynchronous operations in order, and the air runtime environment will execute one by one:

1. var myfile: file = file.doc umentsdirectory. resolvepath ("mousebomb/test.txt ");
2. var myfilestream: filestream = new filestream ();
3. myfilestream. openasync (myfile, filemode. Write );
4. myfilestream. writeutfbytes ("hello ");
5. myfilestream. writeutfbytes ("world ");
6. myfilestream. addeventlistener (event. Close, closehandler );
7. myfilestream. Close ();
8. Trace ("started .");
9. closehandler (Event: Event): void
10 .{
11. Trace ("finished .");
12 .}

Output of this meeting:
Started.
Finished.

You can set the position value immediately after the asynchronous read/write operation is called. The next read/write operation will start from that position. For example:

1. var myfile: file = file.doc umentsdirectory. resolvepath ("mousebomb/test.txt ");
2. var myfilestream: filestream = new filestream ();
3. myfilestream. openasync (myfile, filemode. Update );
4. myfilestream. Position = 4000;
5. Trace (myfilestream. position); // 4000
6. myfilestream. writebytes (mybytearray, 0,200 );
7. myfilestream. Position = 300;
8. Trace (myfilestream. position); // 300

3. Select a proper read/write operation based on the data format.
Each file on the hard disk is a collection of bytes. In as, the data in the file can always be described as bytearray. For example, the following code reads the file data to the bytearray of bytes:

1. var myfile: file = file.doc umentsdirectory. resolvepath ("mousebomb/test.txt ");
2. var myfilestream: filestream = new filestream ();
3. myfilestream. addeventlistener (event. Complete, completed );
4. myfilestream. openasync (myfile, filemode. Read );
5. var Bytes: bytearray = new bytearray ();
6. Function completehandler (Event: Event): void
7 .{
8. myfilestream. readbytes (bytes, 0, myfilestream. bytesavailable );
9 .}

The following code writes data from bytes bytearray to a file:

1. var myfile: file = file.doc umentsdirectory. resolvepath ("mousebomb/test.txt ");
2. var myfilestream: filestream = new filestream ();
3. myfilestream. Open (myfile, filemode. Write );
4. myfilestream. writebytes (bytes, 0, bytes. Length );

We often do not want to process the data as bytearray. Sometimes the file to be processed is in a specific format. For example, the data in the file is a string. Therefore, the filestream class also contains read/write methods in data formats other than bytearray. For example, the readmultibyte () method can save file reading as a string, as shown in the following code:

1. var myfile: file = file.doc umentsdirectory. resolvepath ("mousebomb/test.txt ");
2. var myfilestream: filestream = new filestream ();
3. myfilestream. addeventlistener (event. Complete, completed );
4. myfilestream. openasync (myfile, filemode. Read );
5. var STR: String = "";
6. Function completehandler (Event: Event): void
7 .{
8. Str = myfilestream. readmultibyte (myfilestream. bytesavailable, "iso-8859-1 ");
9 .}

The second parameter of readmultibyte () (in this example, "iso-8859-1") specifies the text format that ActionScript uses to interpret. ActionScript supports general character set encoding, detailed list in http://livedocs.macromedia.com/flex/2/langref/charset-codes.html

The filestream class also has the readutfbytes () method to read data from the read cache using the UTF-8 character set. Because the utf8 character set is variable-length, the data at the end of the read cache is not necessarily a complete character, so do not use the readutfbytes () method (using readmultibyte () in the method for processing the SS event () read variable long character encoding should also follow this option), but should read the complete dataset when the filestream complete event occurs.

Similarly, there are similar write operations writemultibyte () and writeutfbytes (), used to process string objects and text files.

The readutf () and writeutf () Methods read and write text data, but they assume that text data is not widely used in standard text files before the length of text data is specified.
Some UTF-encoded text files start with a UTF-BOM (byte order mark) character, like encoding formats (such as UTF16 and UTF32), also declare the byte order.

The readobject () and writeobject () Methods facilitate data access for complex as objects. message format) encoding. This format is private to ActionScript. Programs other than air, Flash Player, Flash Media Server, and flex data services do not operate on the built-in APIs in this format.
In addition, there are some read/write operations, such as readdouble () and writedouble (). To use these operations, you must ensure that the format of the operated files matches the format.

Generally, the structure of a file is much more complex than that of a text file. For example, an MP3 file contains a compressed data format that can only be interpreted by the MP3 decompression and decoding algorithm. Other files, such as images, databases, and application archives, have different structures. To use as to operate their data, you must have a good understanding of their structure.

So far, the study notes "Air file operations" have ended. All knowledge points are from: official documents.
(If any, please note .)

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.