The DOM XML extension has been part of PHP since version 4 but was completely overhauled in PHP 5. The primary change was to include the DOM functionality within a default installation of PHP, which is to say that no additional libraries or extensions need to be installed or configured to use these functions.
By the Way
DOM stands for Document Object Model. For more information on DOM, visit http://www.w3.org/TR/DOM-Level-2-Core/core.html.
The purpose of DOM functions is to allow you to work with data stored in an XML document using the DOM API. The most basic DOM function is DOMDocument->load(), which creates a new DOM tree from the contents of a file. After the DOM tree is created, you can use other DOM functions to manipulate the data.
In Listing 28.1, DOM functions are used to loop through a DOM tree and retrieve stored values for later display.
Listing 28.1. Loop Through an XML Document Using DOM Functions
In line 2, a new DOM document is created, and in line 3, the contents of books.xml are loaded into this document. The document tree is now accessible through $dom, as you can see in later lines. Line 5 begins the master loop through the document tree, as it places each node of the document into an array called $books.
Line 6 looks for an element called "Book", and processing continues if one is found. Remember, the <Book></Book> tag pair surrounds each entry for a book in the books.xml file. If processing continues, line 8 gathers all the child nodes into an array called $theBook, and the if statements in lines 912 and 1417 look for specific nodes called "Title" and "Author", respectively, and place the values into the variables $theBookTitle and $theBookAuthor for later use.
Line 19 begins a similar if statement, but because this line looks for a node called "Publishing Info" and you know that the <PublishingInfo></PublishingInfo> tag pair contains its own set of child nodes, another looping construct is needed to obtain the information in the next level of data. On line 22, child nodes are found and placed in the array called $thePublishingInfo, and then if statements in lines 2326 and lines 2832 look for specific nodes called "PublisherName" and "PublishedYear", respectively, and place the values into the variables $theBookPublisher and $theBookPublishedYear for later use.
After the loop created in line 8 is closed in line 35, lines 3740 echo a marked-up string to the browser, using values stored in $theBookTitle, $theBookAuthor, $theBookPublisher, and $theBookPublishedYear variables. After these values are used, they are unset in lines 4245, and the loop continues for the next "Book" enTRy in the document tree.
Save this listing as domexample.php and place it in the document root of your web server. When viewed through your web browser you should see something like Figure 28.1.
Figure 28.1. Text extracted and displayed using DOM functions.
For a complete listing of the more than 80 DOM-related functions in PHP, including functions to add to your XML document and save the new version, visit the PHP Manual at http://www.php.net/dom.
In the next section, you'll use the same books.xml file but will retrieve and display its values using the SimpleXML family of functions.