25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

XML and Web Services ” 197<br />

Since <strong>PHP</strong> 5.1.3, SimpleXML has had the ability to return all namespaces declared<br />

in a document, return all namespaces used in a document, and register a<br />

namespace prefix used in making an XPath query. The first of these features is<br />

SimpleXMLElement::getDocNamespaces(), which returns an array of all namespaces<br />

declared in the document. By default, it returns only those namespaces declared<br />

in the root element referenced by the SimpleXMLElement object, but passing true to<br />

the method will cause it to behave recursively and return the namespaces declared<br />

in all children. Since our sample XML document declares four namespaces in the<br />

root element of the document, getDocNamespaces() returns four namespaces:<br />

$namespaces = $library->getDocNamespaces();<br />

foreach ($namespaces as $key => $value)<br />

{<br />

echo "{$key} => {$value}\n";<br />

}<br />

Notice that the foo namespace was listed, but was never actually used. A<br />

call to SimpleXMLElement::getNamespaces() will return an array that only contains<br />

those namespaces that are actually used throughout the document. Like<br />

getDocNamespaces(), this method accepts a boolean value to turn on its recursive behaviour.<br />

$namespaces = $library->getNamespaces(true);<br />

foreach ($namespaces as $key => $value)<br />

{<br />

echo "{$key} => {$value}\n";<br />

}<br />

DOM<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)<br />

The <strong>PHP</strong> 5 DOM extension sounds similar to the <strong>PHP</strong> 4 DOMXML extension, but it<br />

has undergone a complete transformation and is easier to use. Unlike SimpleXML,<br />

DOM can, at times, be cumbersome and unwieldy. However, this is a trade-off for<br />

the power and flexibility it provides. Since SimpleXML and DOM objects are inter-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!