ActionScript 3.0 step by step series (7): use XML and xmllist classes to process XML data

Source: Internet
Author: User
ActionScript 3.0 step by step series (7): use XML and xmllist classes to process XML data

XML-extensible markup language. I want to see that you have already used it. OK. Let's go straight to the topic to see how the XML data is processed in ActionScript 3.0.

In ActionScript 3.0, there are two main classes used to process XML data: XML and xmllist. The former indicates a single XML element. It can be an XML document containing multiple child elements or a single value element in the document. The latter represents a group of XML elements.

Since it is XML, it also has the same terms as C #/Java in ActionScript 3.0, such as documents, elements, nodes, and attributes. This document focuses on creating, adding, deleting, querying, and loading external XML files of XML documents. It describes how to process XML in ActionScript 3.0.

Before that, let's take a look at the E4X method. What is the ecmascript for XML (E4X) method? It is an operator provided in ActionScript 3.0 and is used to access and process XML data. ActionScript 3.0 contains the following E4X classes: XML, xmllist, QNAME, and namespace. They are stored in the Flash. xml package to make it easier to write and understandCode.

 
1. Create an internal XML document

It is very easy to create an XML document under Flex. You can use an XML object to create it directly, as shown below:

1 Private VaR pxml: XML =   < Root >  
2 < Book ID = " 1 " >
3 < Name > Getting started with ActionScript </ Name >
4 < Author > Zhang San </ Author >
5 < Price > 48.50 </ Price >
6 </ Book >
7 < Book ID = " 2 " >
8 < Name > ASP. NET Advanced Programming </ Name >
9 < Author > Li Si </ Author >
10 < Price > 86.70 </ Price >
11 </ Book >
12 </ Root >

 

Ii. Load External XML documents

In addition to the above method of creating an XML document, we can also use an external XML document (this XML document can be an XML file, can also be the return value of a function). To load an external XML file, you can use the <mx: XML> label in flex, as shown below:

 

1 < MX: XML ID = " Book " Source = " Data/book. xml " >
2
3 </ MX: XML >

The XML document loaded in the preceding method can be directly bound to interface elements through the book instance during interface design. To use external XML in programming, We need to load XML in another way, using both urlloader. load () method, and you also need to set the dataformat attribute to dataformat. text reads data and converts the loaded data to an XML instance using the complete event processing function. The following code block:

 

1 Private Function loadxml (): Void  
2 {
3 // Create a urllloader instance to read data in the form of simple text
4 // Set the data loading format to dataformat. Text
5 // Listen to and add the complete event processing function
6 VaR Loader: urlloader =   New Urlloader ();
7 Loader. dataformat = Dataformat. text;
8 Loader. addeventlistener (event. Complete, handlercomplete );
9 Loader. Load ( New URLRequest ( " Data/book. xml " ));
10 }
11
12 Private Function handlercomplete ( Event : Event ): Void
13 {
14 VaR book: XML =   New XML ( Event . Target. data );
15 }

In addition to loading external XML data there is also a kind of RPC, in some cases also called XML-RPC, the common development mode is usually provided for the server to call the interface, the interface can be ASP, JSP, PHP, aspx, webService, WCF, and so on can all be provided. I will not describe it here.Article.

Iii. XML elements, nodes, and attributes

Create sub-elements using the E4X syntax in ActionScript 3.0 and add them to XML. The XML Object provides two very useful methods: The insertchildbefort () method and the insertchildafter () method, both methods add elements based on the current element.

To add attributes to an XML document node, you must use the @ operator. It is easy to use, as shown below:

1 Internal Function onclick (): Void  
2 {
3 VaR book: XML =   < Book >
4 < Item > </ Item >
5 </ Book > ;
6 Book. item. @ ID = 1 ;
7 Book. item. @ name = " Event 3.0 " ;
8
9 Alert. Show (book );
10 }

The XML object data after adding the attribute is as follows:

4. Use xmllist to read XML data

The XML Object provides a method for returning the xmllist type Elements (), which returns the child element nodes of all XML objects, then we can use the for each statement loop to access the entire XML data content. The preceding example shows how to create XML data in an internal XML document. For details, see the following code block:

1 VaR list: xmllist = Pxml. Elements ();
2 For Each (VAR element: XML In List)
3 {
4 // Obtain the ID of each node
5 Alert. Show (element. @ ID );
6 Trace (element. @ ID );
7 }

 

Note that the elements () method only applies to the next-level nodes of the current node. If there are many XML hierarchies, recursive processing is required. You can also use the "." operator to retrieve data. The format is root node. subnode... element name;

For details, please refer to the translation notes of "ActionScript 3.0 cookbook", which is very clear.

 

From: http://www.cnblogs.com/beniao/archive/2009/01/03/1363859.html

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.