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 ” 203<br />

Removing Data<br />

There are three types of data you may want to remove from an XML document:<br />

attributes, elements and CDATA. DOM provides a different method for<br />

each of these tasks: DomNode::removeAttribute(), DomNode::removeChild() and<br />

DomCharacterData::deleteData():<br />

$xml = item(0)->parentNode->removeChild($result->item(0));<br />

$result->item(1)->removeAttribute(’type’);<br />

$result = $xpath->query(’text()’, $result->item(2));<br />

$result->item(0)->deleteData(0, $result->item(0)->length);<br />

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

In this example, we start by retrieving all of the text nodes from our document,<br />

then we remove the first one by accessing its parent and passing the former to<br />

DomNode::removeChild(). Next, we remove the type attribute from the second element<br />

using DomNode->removeAttribute().<br />

Finally, using the third element, we use Xpath again to query for the corresponding<br />

text() node, passing in the third element as the context argument, and then delete<br />

the CDATA using DomCharacterData::deleteData(), passing in an offset of 0 and a<br />

count that is the same as the length of the CDATA node.<br />

Licensed to 482634 - Amber Barrow (itsadmin@deakin.edu.au)

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

Saved successfully!

Ooh no, something went wrong!