25.09.2014 Views

ZEND PHP 5 Certification STUDY GUIDE

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

202 ” XML and Web Services<br />

$dom = new DOMDocument();<br />

$dom->load("library.xml");<br />

$xpath = new DomXPath($dom);<br />

$xpath->registerNamespace("lib", "http://example.org/library");<br />

$result = $xpath->query("//lib:book");<br />

$clone = $result->item(0)->cloneNode();<br />

$result->item(1)->parentNode->appendChild($clone);<br />

Modifying Data<br />

When modifying data, you typically want to edit the CDATA within a node. Apart<br />

from using the methods shown above, you can use XPath to find a CDATA node and<br />

modify its contents directly:<br />

$xml = item(0);<br />

$node->data = ucwords($node->data);<br />

echo $dom->saveXML();<br />

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

In this example, we apply ucwords() to the text() node’s data property. The transformation<br />

is applied to the original document, resulting in the following output:<br />

<br />

<br />

Some Text Here<br />

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

Saved successfully!

Ooh no, something went wrong!