One of more powerful features of ASDK is a very easy to use XML parser that also includes support for XML namespaces, a feature absent from the standard XML API that ships with Flash. Working with XML using ASDK provides a more natural experience by surfacing a clean object oriented API of an XML document. This API includes common classes such as Document, Element, and Attribute along with several supporting classes within the asdk.xml package. More details regarding ASDKs support for XML is discussed in the XML Parser section of the guide. The example below demonstrates how to use the API to parse the purchase order XML document that is available here.
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 |