var http = loadXMLObject("people.xml"); var personNodes = http.documentElement.getElementsByTagName("person"); var ageNodes = http.documentElement.getElementsByTagName("age");
The personNodes variable becomes an array which contains references to the two person nodes, and ageNodes contains the two ageNodes that exist. If we were to execute the code above and then the following code, however...
var firstPersonNameNode = personNodes.getElementsByTagName("name");
To access an attribute, you can use the getAttribute function, which takes the name of the attribute you want to get as a parameter. This function is used on the same node you'd access the attributes array on.
var nameNode = http.firstChild.firstChild; var prefixAttrib = nameNode.getAttribute("prefix");
After this code executes, prefixAttrib will equal "Mr".
With the power of these functions at your disposal, getting the information you want from your XML files will be as easy as invoking a function. Beware, though, of obtaining all person nodes with the getElementsByTagName function and then trying to get to the name and age nodes from the resulting array. Why? The same problems with FireFox exist: you'll have to parse out all of the empty nodes before you'll be able to get at the real information. To prevent that, look for the nodes that directly contain the information: i.e., the name and age nodes in our case. Those are the only nodes what will have no empty child nodes before their text nodes.
In the next lesson, we'll learn to add and remove nodes using the various functions available to us.