5.3 DOM

 <  Day Day Up  >  

DOM is one of PHP 5's two major XML processing extensions. This section introduces DOM, providing an overview of how it organizes information. It also demonstrates how to turn XML documents into DOM objects and vice versa.

5.3.1 About DOM

There is only one way to read XML into a tree using PHP 4: DOM. DOM, short for Document Object Model, is a W3C standard describing a platform- and language-neutral interface for interacting with XML and other structured documents. DOM then provides a series of utility functions to recurse through the branches and pick out the nodes and data that you want.

It's easier to parse XML into a tree than to use a streaming parser such as SAX. When you read XML into a tree, you can move through the document in a way that is similar to navigating PHP data structures such as multi-dimensional arrays or objects that have subobjects. However, if your document is large, DOM can use lot of memory.

PHP 5's DOM utilities have undergone a complete rewrite. If you used the DOM functions in PHP 4, you know that PHP's DOM support has largely been sketchy and incomplete. The original implementation did not conform at all to the W3C naming conventions, thus partially defeating the purpose of a language-neutral API. Although PHP 4.3 unveiled an improved and more compliant set of DOM functions, there are still holes and memory leaks.

On top (or perhaps because) of all this, a large "EXPERIMENTAL" tag had seemingly been permanently placed upon PHP 4's DOM functions. When the warning "The behavior of this extension ”including the names of its functions and anything else documented about this extension ”may change without notice in a future release of PHP. Use this extension at your own risk" appears at the top of documentation, it does not engender comfort .

Happily, all that has changed in PHP 5. The new DOM extension not only has updated internals, but you now interact with it in the standard way, and it has a few new features, such as validation. Still, the entire DOM specification is quite large and complex, and not all features are available yet. But what has been implemented is done correctly and is consistent with other languages.

Unfortunately, if you've written any applications that use the old DOM extension, they won't work with PHP 5. You must update them.

5.3.2 Turning XML Documents into DOM objects

Before you can do anything DOM- related in PHP 5, you need to create a new instance of the DOM object, called DOMDocument :

 $dom = new DOMDocument; 

Now you can load XML into $dom . DOM differentiates between XML stored as a string and XML stored in a file. To read from a string, call loadXML( ) ; to read from a file, call load( ) :

 $dom = new DOMDocument; // read from a string $dom->loadXML('<string>I am XML</string>'); // read from a file $dom->load('address-book.xml'); 

The DOM load( ) method, like all XML input and output methods , actually works for more than just files. It really works with streams, so it can read from HTTP or write to FTP. See Chapter 8 for more information about streams.

If DOM encounters problems reading the XML ”for example, the XML is not well- formed , your file does not exist, or you try to pass in an array instead of a string ”DOM emits a warning. In some cases, such as a failure of the DOM extension to create a new DOM object or safe_mode blocking off access to the file, it returns false instead.

This example tries to load a string that's invalid XML:

 $dom = new DOMDocument; // read non well-formed XML document $dom->loadXML('I am not XML'); 

It causes DOM to give a PHP Warning that begins like this:

  PHP Warning:  DOMDocument::loadXML( ): Start tag expected, '<' not found  

Whitespace is considered significant in XML, so spaces between tags are considered text elements. For example, there are five elements inside the person element:

 <person>         <firstname>Rasmus</firstname>         <lastname>Lerdorf</lastname> </person> 

It looks like there are only two elements, firstname and lastname , but there are actually three additional text nodes. They're hard to see because they're whitespace. They occur between the opening person tag and the opening firstname tag, the closing firstname tag and opening lastname tag, and the closing lastname and closing person tag.

However, removing the whitespace makes the document hard for humans to read. Happily, you can tell DOM to ignore whitespace:

 $dom = new DOMDocument; // Whitespace is no longer significant $dom->preserveWhiteSpace = false; $dom->loadXML('<string>I am XML</string>'); 

Setting the preserveWhiteSpace attribute to false makes DOM skip over any text nodes that contain only spaces, tabs, returns, or other whitespace.

5.3.3 DOM Nodes

DOM organizes XML documents into nodes. You can use DOM to retrieve the text stored in a node, find a node's children, insert another node at that location, and so forth.

Figure 5-1 shows how DOM represents the beginning of the address book.

Figure 5-1. A DOM representation of an XML address book
figs/uphp_0501.gif

5.3.3.1 Accessing the root element

The root element of an XML document is stored as the documentElement property of a DOM object:

 $dom = new DOMDocument; $dom->preserveWhiteSpace = false; $dom->load('address-book.xml'); $root = $dom->documentElement; 

The $root variable now holds a pointer to the document root.

5.3.3.2 Navigating through nodes

DOM has a whole set of tree iteration properties that allow you to explicitly move from one element to another. In PHP 4, these are object methods, but they're object properties in PHP 5.

The easiest way to process all of a node's children is with a foreach upon its childNodes . For example, to process all the person elements in the address book:

 $dom = new DOMDocument; $dom->preserveWhiteSpace = false; $dom->load('address-book.xml'); $root = $dom->documentElement; foreach ($root->childNodes as $person) {     process($person); } 

The childNodes attribute is not an array, but a DOMNodeList object. The item( ) method allows you to access individual items, and the length property tells you the number of items in the list.

This code is equivalent to the foreach loop:

 $people = $root->childNodes; for ($i = 0; $i < $people->length; $i++) {     process($people->item($i)); } 

The first element lives in position , the second in 1 , and so on.

Table 5-1 contains the complete list of properties and what they do.

Table 5-1. DOM iteration properties

PHP 5 property

PHP 4 method

Description

parentNode

parent_node( )

The node above the current node

childNodes

child_nodes( )

A list of nodes below the current node

firstChild

first_child( )

The "first" node below the current node

lastChild

last_child( )

The "last" node below the current node

previousSibling

previous_sibling( )

The node "before" the current node

nextSibling

next_sibling( )

The node "after" the current node


5.3.3.3 Determining node types

libxml2 has 21 different types of nodes. The most frequently encountered types are elements, attributes, and text. The nodeType method returns a number describing the node.

For instance, the documentElement is always an element:

 $root = $dom->documentElement; print $root->nodeType;  1  

Table 5-2 lists libxml2 's node types.

Table 5-2. libxml2's numeric node types

Node type

Number

Element

1

Attribute

2

Text

3

CDATA section

4

Entity reference

5

Entity

6

PI (Processing Instruction)

7

Comment

8

Document

9

Document type

10

Document fragment

11

Notation

12

HTML document

13

DTD

14

Element declaration

15

Attribute declaration

16

Entity declaration

17

Namespace declaration

18

XInclude start

19

XInclude end

20

DocBook document

21


5.3.3.4 Accessing text nodes

DOM never makes any assumptions about how your data is organized or what you wish to do with it. If you have a snippet of XML that looks like this:

 <firstname>Rasmus</firstname> 

DOM does not assume that your primary interest is the string Rasmus . To DOM, Rasmus is just the text portion of a child node associated with the node for the firstname element.

Here's how to access Rasmus :

 // load in XML $rasmus = newDOMDocument; $rasmus->loadXML('<firstname>Rasmus</firstname>'); // two DOM longhand ways $rasmus->firstChild->firstChild->nodeValue; $rasmus->firstChild->firstChild->data; // the first element of the children method $rasmus->childNodes->item(0)->nodeValue; // a DOM shorthand way of saying the same thing $rasmus->firstChild->nodeValue; // a PHP 5 shorthand method // *NOT* portable across DOM implementations $rasmus->textContent; // yet another way, because this is the root element $rasmus->documentElement->nodeValue; 

DOM does not couple the element with the text wrapped by its tags. Therefore, you must ask the node for its first child. This gives you the text node holding PHP . However, you can't print the node, because it's an object, not a string. To access the text portion of a text node, you need to grab its nodeValue .

PHP 5's DOM implementation has a special attribute textContent that's equivalent to firstChild->nodeValue . This attribute name is shorter, but it is not portable, because it's not part of the DOM standard.

5.3.3.5 Accessing element nodes

DOM stores an element's name in the tagName property. This code loops through a person element from the address book and prints out the names of all the elements and the values of their first children:

 $dom = new DOMDocument; $dom->preserveWhiteSpace = false; $dom->load('address-book.xml'); $person = $dom->documentElement->firstChild; foreach ($person->childNodes as $field) {     if ($field->nodeType =  = 1) {         print "$field->tagName: {$field->firstChild->nodeValue}\n";     } }  firstname: Rasmus   lastname: Lerdorf   city: Sunnyvale   state: CA   email: rasmus@php.net  

The $person object holds the person node, and its children are the address book fields.

Inside the foreach , you need to check the nodeType to make sure you have an element node. All elements in libxml2 have a nodeType of 1 . Skipping this check processes the comment node because DOM does not ignore comments.

5.3.4 Turning DOM Objects into XML Documents

To take a DOM document and convert it back into XML, you have two options: save( ) and saveXML( ) . The first method saves a document to a file; the other returns a string representation of the document, which you can print out or store in a variable.

 $dom->save('address-book.xml'); print $dom->saveXML( ); 

As always, you must have write permission for the directory in which you're saving the file.

If you disable the preserveWhiteSpace attribute, your XML ends up as a single line:

 $dom = new DOMDocument; $dom->preserveWhiteSpace = false; $dom->load('address-book.xml'); print $dom->saveXML( );  <address-book><person id="1"><!--Rasmus Lerdorf--><firstname>Rasmus</fi   rstname><lastname>Lerdorf</lastname><city>Sunnyvale</city><state>CA</st   ate><email>rasmus@php.net</email></person><person id="2"><!--Zeev Suras   ki--><firstname>Zeev</firstname><lastname>Suraski</lastname><city>Tel A   viv</city><state/><email>zeev@php.net</email></person></address-book>  

To prevent this, set the formatOutput attribute to true :

 $dom = new DOMDocument; $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->load('address-book.xml'); print $dom->saveXML( );  <?xml version="1.0"?>   <address-book>   <person id="1">   <!--Rasmus Lerdorf-->   <firstname>Rasmus</firstname>   <lastname>Lerdorf</lastname>   <city>Sunnyvale</city>   <state>CA</state>   <email>rasmus@php.net</email>   </person>   <person id="2">   <!--Zeev Suraski-->   <firstname>Zeev</firstname>   <lastname>Suraski</lastname>   <city>Tel Aviv</city>   <state/>   <email>zeev@php.net</email>   </person>   </address-book>  

Now the elements are indented. However, since libxml2 does not indent comments, those nodes remain on the left.

 <  Day Day Up  >  


Upgrading to PHP 5
Upgrading to PHP 5
ISBN: 0596006365
EAN: 2147483647
Year: 2004
Pages: 144

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