XML Parser

Parsing XML

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.*;
import asdk.xml.*;
var xmlUrl = "http://www.themidnightcoders.com/asdk/purchaseorder.xml";
Document.load( xmlUrl, documentLoaded );

function documentLoaded( document )
{
  var root = document.getRoot();
  var elements = root.getElements();

  while( elements.hasNext() )
  {
     var element = elements.getNext();
     var name = element.getName();
     trace( "element name = " + name );

     switch( name )
     {
       case "billTo":
         processAddress( element );
         break;
       case "shipTo":
         processAddress( element );
         break;
       case "comment":
         trace( "comment = " + element.getString() );
         break;
       case "items":
         processItems( element );
         break;
     }
  }
}

function processAddress( address )
{
  trace( "***** processing address *****" );
  var country = address.getAttribute( "country" );
  trace( "attribute " + country.getName() + " has value " + country.getValue() );
  var elements = address.getElements();

  while( elements.hasNext() )
  {
    var element = elements.getNext();
    trace( "element " + element.getName() + " has value of " + element.getString() );
  }
}

function processItems( itemsElement )
{
  trace( "***** processing items *****" );
  var items = itemsElement.getElements();

  while( items.hasNext() )
  {
    var item = items.getNext();
    var partNum = item.getAttribute( "partNum" );
    trace( "attribute " + partNum.getName() + " has value " + partNum.getValue() );
    var elements = item.getElements();

      while( elements.hasNext() )
      {
        var element = elements.getNext();
        trace( "element " + element.getName() + " has value of " + element.getString() );
      }
  }
}

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
***** processing address *****
attribute country has value US
element name has value of Alice Smith
element street has value of 123 Maple Street
element city has value of Mill Valley
element state has value of CA
element zip has value of 90952
element name = billTo
***** processing address *****
attribute country has value US
element name has value of Robert Smith
element street has value of 8 Oak Avenue
element city has value of Old Town
element state has value of PA
element zip has value of 95819
element name = comment
comment = Hurry, my lawn is going wild
element name = items
***** processing items *****
attribute partNum has value 872-AA
element productName has value of Lawnmower
element quantity has value of 1
element USPrice has value of 148.95
element comment has value of Confirm this is electric
attribute partNum has value 926-AA
element productName has value of Baby Monitor
element quantity has value of 1
element USPrice has value of 39.98
element shipDate has value of 1999-05-21