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 understand the code used to process XML data.
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> James </Author>
<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 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.tar get. 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 them here. I will introduce them in detail in future articles.
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 = "ActionScript 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.