Accessing nodes

Our previous example for xml was very simple. This is ok to explain what XML is but it is not really realistic, because databases are very large with different data and therefore much more complex with substructures. So let« look at a more complex XML file, which we will develop further and finally create a database with a simple search engine. I will not post the whole XML file here but instead click on the link to see the file. The structure more simplified is like this:

                   <?xml version="1.0"?>
                    <models>//childNodes from here on
	                    <female>//firstChild
	                        <black>//firstChild.firstChild
                                  <susan>//firstChild.firstChild.firstChild
                                   Susan Black//textnode or nodeValue
                                   age: 21
                                   eye color: dark blue
                                   hair color: black
                                   foreign language: german
	                              </susan>
	                               more nodes here//firstChild.firstChild.nextSibling
	                        </black>
	                        <blond>
                                   more nodes here      
	                        </blond>
	                     </female>
	                     <male>//lastChild
	                        <black>//lastChild.firstChild
                                   more nodes here
	                        </black>
	                        <blond>
                                   more nodes here
	                        </blond>
	                      </male>
                      </models>
                      
We have of course as you have seen more textnodes, which I have omitted for simplification. I have indicated the location of all the individual nodes here. All nodes are childnodes of the rootnode. So how do we attack this xml file to show the nodes?
One possibility is to attack each node individually, but imagine what kind of code work that is to write child of child of .... . So the way to do this is by using loops, which will go through all the children that we can easily grab those nodes we want. In the following the script is shown to get to the female or male node, for which we use the same script.

                    function listItems(myItem01:XMLNode){
                         _root.textField1.html=true;
                         _root.textField2.html=true;
                         _root.textField3.html=true;
                         for (var count01:String in myItem01.childNodes) {
                              var modelList:String = myItem01.childNodes[count01].nodeName;
                              if (modelList == _root.inputTextfield.text) {
                                   var idList:XMLNode = myItem01.childNodes[count01];
                                   _root.mainField.text = idList;
                                   _root.textField1.text = idList.firstChild;
                                   _root.textField2.text = idList.firstChild.firstChild;
                                   _root.textField3.text = idList.firstChild.firstChild.nextSibling;
                              }
                         }
                    }           
               }
          }
          
I have omitted the first part of the script because it is basically identical to the previous script to load the XML file. We have just added one more line of script to declare private var inputTextfield:TextField;. The main part of the above script is the for ... in ...loop. We loop through childNodes of the nodes, whose nodeName is identical to the one typed in the input textfield, which is either male or female. The var count01 counts the number of childNodes, which are 2 (0 and 1) in this case. Note here that count01 in a for loop is actually not a number but of type String. After looping we create the var modelList, which holds the different nodeNames (myItem01.childNodes[count01].nodeName). This var can be either be of type XMLNode or of type String. Both is parsed without any error message. However, if we want to be correct we better give it the property of a String, since that is what we type in the input textfield. See the next line, which is an if statement. If female then all the female childnodes as held in the var will be shown and if male all the male childnodes will be shown. Before we continue let«s see the real live example below.
This is now a good example to learn a bit more about the xml tree and how ro reach to individual nodes. We already know firstChild. There is of course also lastChild. Then in case we have equal childnodes following the firstChild they are all nextSiblings or depending on their position previousSiblings. And now we are a whole step further. In fact this is already a little search engine with just a few lines of Actionscript.
In the next lesson we will go a little bit deeper into the XML file. It is actually not so much new but you have to digest first what you learnt in this part of the tutorial.
PREVIOUS          NEXT