Preparing the data

A relational network represents hierarchy and linkage between objects and a good data structure, such as the tree data structure. This recipe is going to show how to parse the data for it to be ready to generate a relational network.

Getting ready

Open the files downloaded from the Packt Publishing website for Chapter 8 | Recipe 1 to follow along.

How to do it...

The following steps will show you how to parse the data for a relational network:

  1. Create the data in an XML file similar to Data.xml. The following is what one entry looks like:
    <node name="NODE NAME" color="0x0000ff"></node>
  2. In Main.as, add the embed code to attach the data to our project:
    [Embed(source = "/Data.xml", mimeType="application/octet-stream")]
    private var XmlData:Class;
  3. Load the XML file using the ByteArray.readUTFbytes function:
    var byteArray:ByteArray = (new XmlData()) as ByteArray;
    var xml:XML = XML(String(byteArray.readUTFBytes( byteArray.length )));
    var xmlList:XMLList = xml.children();
  4. Create the Node.as class that will hold the data. It has a name, a color, a parent, and a child field.
  5. Create the recursive function, in Main.as, that will go through the XML data:
    private function _createNodes(parent:Node, xml:XML):Node {
      var node:Node = new Node();
      node.parent = parent;
      node.name = xml.@name;
      node.color = xml.@color;
      var children:XMLList = xml.children();
      for (var i:int = 0; i < children.length(); i++) {
        node.children.push(_createNodes(node, children[i]));
      }
      return node;
    }
  6. Feed the XML data to the _createNodes function and keep a link to the root node:
    var data:Node = _createNodes(null, xmlList[0]);

How it works...

For this recipe, we are not going to store the data in a vector or an array, but we are going to build a tree data structure to hold it. What we are going to build is very similar to a linked list—well, actually more similar to a double linked list. Our data class will be a node. It holds the data, but it also holds pointers to its parent and to its children.

Indeed most nodes are going to be parents to other nodes and have other nodes as children. A node is a parent of another node if it has a link to it in its children Vector. The first node has no parent and is called the root . If a node has no children it is called a leaf.

JSON is always an easy solution to store the data as it is smaller and easily convertible in an ActionScript Object type but, for this particular recipe, XML is actually better to work with. It is very good at representing the relation between parent and child, and with a quick glance at it you get a good understanding of the structure of the tree.

Using recursion is great when dealing with trees. Recursion is when a function does something and then calls itself again until an end condition is met. In this case, since there is not much difference between nodes whether they are children or parents (or both), we can use the same function to parse them. The function we created for that is _createNodes. It creates a new node and then calls _createNodes for all of its children. Since the function _createNodes returns Node, we can save those in a vector. We do this until a node doesn't have a child.

There's more...

Linked lists and trees are extensive subjects. Adding these data structures to your toolkit will be extremely useful as they are used often.

Links for more theory

Wikipedia has good information about linked lists available at:

http://en.wikipedia.org/wiki/Linked_list

For more information on double linked lists, refer to:

http://en.wikipedia.org/wiki/Doubly_linked_list

For more information on trees:

http://en.wikipedia.org/wiki/Tree_(data_structure)

See also

  • The Parsing data to use as region fill recipe in Chapter 6, Mapping Geographical and Spatial Data.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.221.133