Power of Attributes

So far we have learnt that childNodes are XML elements or textnodes. We have learnt what is the relationship of childnodes to each other: parent, child, firstChild, lastChild, nextSibling or previousSibling. But one element is missing, which is the attribute. Before we go further, we will look at the syntax of the attribute.

                                 <?xml version="1.0"?>
                                 <models>
                                     <susan name="Susan Black" language="german">//name and language attributes
                                  Susan Black
                                  age: 21
                                  eye color: dark blue
                                  hair color: black
                                  foreign language: german
                                      </susan>
                                  <models>
                                  
Attributes are added to nodes and the attribute content is always in quotations. Each attribute of one node should always have its unique name. We should not name Susan Black and german with the same attribute unless we put the text together under one attribute name. How do we parse XML to get to attributes? This is shown in the actionscript below.

                 function listItems(model01:XMLNode){
                       var fItem:Object = model01.firstChild.attributes;
                       _root.textField1.text = fItem.name;
                 }
                         
We just add the attributes property to a node name. If the node has an attribute we have to specify it by naming the attribute as shown here with model01.firstChild.attributes.name. If we want only lower case we add that: model01.firstChild.attributes.name.toLowerCase();. However, we are now dealing with Actionscript 2 syntax and as you may have noticed looking at the var fItem that it is of type Object. Below is shown how that little xml file will be parsed by the script.
Before we create our final search engine file we need to learn about the use of HTML in XML and how the flashplayer handles that.
PREVIOUS          NEXT