Accessing XML in PHP Using DOM Functions


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

 1:  <?php 2:  $dom = new DomDocument; 3:  $dom->load("books.xml"); 4: 5:  foreach ($dom->documentElement->childNodes as $books) { 6:      if (($books->nodeType == 1) && ($books->nodeName == "Book")) { 7: 8:          foreach ($books->childNodes  as $theBook) { 9:              if (($theBook->nodeType == 1) && 10:             ($theBook->nodeName == "Title")) { 11:                 $theBookTitle = $theBook->textContent; 12:             } 13: 14:             if (($theBook->nodeType == 1) && 15:             ($theBook->nodeName == "Author")) { 16:                 $theBookAuthor = $theBook->textContent; 17:             } 18: 19:             if (($theBook->nodeType == 1) && 20:             ($theBook->nodeName == "PublishingInfo")) { 21: 22:                 foreach ($theBook->childNodes as $thePublishingInfo) { 23:                     if (($thePublishingInfo->nodeType == 1) && 24:                     ($thePublishingInfo->nodeName == "PublisherName")) { 25:                         $theBookPublisher = $thePublishingInfo->textContent; 26:                        } 27: 28:                     if (($thePublishingInfo->nodeType == 1) && 29:                     ($thePublishingInfo->nodeName == "PublishedYear")) { 30:                          $theBookPublishedYear = 31:                             $thePublishingInfo->textContent; 32:                        } 33:                 } 34:             } 35:         } 36: 37:         echo " 38:         <p><span style=\"text-decoration:underline\">".$theBookTitle."</span> 39:         by ".$theBookAuthor."<br/> 40:         published by ".$theBookPublisher." in ".$theBookPublishedYear."</p>"; 41: 42:         unset($theBookTitle); 43:         unset($theBookAuthor); 44:         unset($theBookPublisher); 45:         unset($theBookPublishedYear); 46:     } 47: } 48:?>

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.




Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net