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.
Open the files downloaded from the Packt Publishing website for Chapter 8 | Recipe 1 to follow along.
The following steps will show you how to parse the data for a relational network:
Data.xml
. The following is what one entry looks like:<node name="NODE NAME" color="0x0000ff"></node>
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;
ByteArray.readUTFbytes
function:var byteArray:ByteArray = (new XmlData()) as ByteArray; var xml:XML = XML(String(byteArray.readUTFBytes( byteArray.length ))); var xmlList:XMLList = xml.children();
Node.as
class that will hold the data. It has a name, a color, a parent, and a child field.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; }
_createNodes
function and keep a link to the root node:var data:Node = _createNodes(null, xmlList[0]);
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.
Linked lists and trees are extensive subjects. Adding these data structures to your toolkit will be extremely useful as they are used often.
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
3.15.221.133