Overview
This example shows how to navigate a DOM document.
Initiation
The first part of the code (rows 8-10) creates the DOM document object. The DOM object has all the functionality needed to
load and parse a document. We use the load function (row 10) to load the data into memory. When this is done, the DOM object contains
a reference to the data that has been loaded into memory.
Element Navigation
The documentElement (row 12) attribute of the DOMDocument object contains a reference
to the root node of the dom document. In this case it is a person element.
In the following row (row 12) we print the name of the element using the tagName attribute (for this file example1.xml the root element is a person element)
The foreach statement on row (13-17) iterates over the childNodes of the root node that was retrieved on row 12.
The childNodes attribute returns an array of the child nodes of the node. If there are no child nodes, an empty array is returned.
The nodeValue attribute (row 16) of a document element gives us the text content of any elements
below the referenced element. In this case the nodeValue for the "car" element returns the value "Red!!" even though the contents inside the car element
is a color element which contains the text "Red!!".
Web Page Output: