The purpose of XML is to structure data in a meaninful and easy to process format and its power lies primarily in its ubiquity. XML is supported by a wide variety of programming lanuages which makes it especially useful for passing data between applications. It could also be used for configuration allowing a Flash application to function dynamically based on the contents of an XML file read from the server.
ASDK XML aims to make the process of using XML within a Flash application easier. It does this by providing and XML centric API that focuses on the most widespread use cases for using XML: getting data quickly and easily. The example below demonstrates the primary features of ASDK XML when parsing and extracting data from an XML file. It depends on the the purchase order XML document that is available here that highlights typical usage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
import asdk.*; |
Note on line one that the asdk.xml package is imported into the script; this allows Flash access to all the classes that ASDK uses to provide XML support. The action here begins on line 4 when the static load method of the Document class is called. This method takes a string (the path to an XML document) and a callback function as parameters. The Document is then parsed and the callback function is invoked by ASDK to pass the document back to your application. It is then a simple matter to traverse the document by iterating over the elements of the document. In this particular example, the root element of the document is retrieved (line 8) and all of its children are returned from the call to root.getElements (line 9). This returns an asdk.xml.Elements object which conforms to the asdk.Iterator interface. This object allows for easy traversal of series of children elements using the while hasNext, getNext idiom (lines 11/13, 42/44, 52/54, and 61/63).
Also note that the attributes for an element are available by using the getAttribute method (lines 38 and 57) by passing in the name of the attribute. Assuming the attribute exists, this call returns an instance of asdk.xml.Attribute that surfaces two primary methods: getName and getValue that returns the name and the value of the attribute respectively.
Lastly observe that getting the data from an XML element is as simple as calling getString on the element (lines 45 and 64).
Running the example above within the Flash IDE will yield the following line in the Output window:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
element name = shipTo |