Recursive XML Parsing

I l @ ve RuBoard

The problem with the preceding task is that it depends on specific pieces of information being at specific node locations. What's the point of calling a node email if you are not even going to refer to it by name ? Besides, what if the creator of the XML file accidentally moves the ssnumber tag to the end of the user ? Then everything is out of whack, and your code won't find the right values at the right node numbers .

Fortunately, there is a way to parse an XML file one node at a time, check what is there, and populate fields based on the name of the node.

XML is recursive. This means that an XML document is an XML document. A node in that XML document is also an XML document. A node in that XML document is also an XML document and so on.

You can write code that parses an XML document using a recursive function. This function is told to examine the current XML document and determine whether it is an element or a text node. If it is a text node, it is simply data that needs to be placed somewhere. If it is an element, it is a smaller XML document that needs to be parsed.

To explain further, let's look at the code:

 function parseData(dataPart,parent,grandparent) {     if (dataPart.nodeType == 3) { // get value of text node         this [grandparent+" "+parent] = dataPart.nodeValue;     }  else if (dataPart.nodeType == 1) {         // loop through attributes         for(var attr in dataPart.attributes) {             this[dataPart.nodeName+" "+attr] = dataPart.attributes[attr];         }         // loop through child nodes         for(var i=0;i<dataPart.childNodes.length;i++) {             parseData(dataPart.childNodes[i],dataPart.nodeName,parent);         }     } } 

This function looks at the XML object passed in, which is in the parameter dataPart . If it is type 3, it is a text node. In that case, it places the text into a text field that has the name as its grandparent and parent. For instance, the text node in the last node has the parent last and the grandparent name . So the name of the text field should be name last . That is where the text would go.

If the XML object is not just a text node, two loops occur. The first looks for any attributes of the node. It puts the contents of the attribute in a text field named after the parent and the attribute name. So the emergencycontact node has the attribute relation . It's content will be put in the emergencycontact relation text field.

The second loop looks at all the child nodes in the node. For each of these, it calls parseData with the new node. It sends in the parent's name as the grandparent and the current node name as the parent. This is the recursive bit. The function is calling itself.

By doing recursion, the program is breaking the XML object into smaller and smaller pieces until it has piece so small that they are just text nodes. It uses the name of the text node's parent and grandparent to determine what text field to place the text node value into.

This little function does the same job as the parseData function in the previous task. However, it never refers to any node by number. So the XML document can be in any order and even contain additional, unused information. If information is missing, the text fields will simply be left blank.

Check out the movie 19xmlrecursivedisplay.fla to see this one in action. Note that the first time parseData is called in the onLoad function, it is sent list and xmldoc as its parent and grandparent. These really aren't important because the text nodes are much farther down.

I l @ ve RuBoard

Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272 © 2008-2017.
If you may any questions please contact us: