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.

204 ” XML and Web Services<br />

Working With Namespaces<br />

DOM is more than capable to handle namespaces on its own and, typically, you can,<br />

for the most part, ignore them and pass attribute and element names with the appropriate<br />

prefix directly to most DOM functions:<br />

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

$node = $dom->createElement(’ns1:somenode’);<br />

$node->setAttribute(’ns2:someattribute’, ’somevalue’);<br />

$node2 = $dom->createElement(’ns3:anothernode’);<br />

$node->appendChild($node2);<br />

// Set xmlns:* attributes<br />

$node->setAttribute(’xmlns:ns1’, ’http://example.org/ns1’);<br />

$node->setAttribute(’xmlns:ns2’, ’http://example.org/ns2’);<br />

$node->setAttribute(’xmlns:ns3’, ’http://example.org/ns3’);<br />

$dom->appendChild($node);<br />

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

We can try to simplify the use of namespaces somewhat by using the<br />

DomDocument::createElementNS() and DomNode::setAttributeNS() methods:<br />

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

$node = $dom->createElementNS(’http://example.org/ns1’, ’ns1:somenode’);<br />

$node->setAttributeNS(’http://example.org/ns2’, ’ns2:someattribute’, ’somevalue’<br />

);<br />

$node2 = $dom->createElementNS(’http://example.org/ns3’, ’ns3:anothernode’);<br />

$node3 = $dom->createElementNS(’http://example.org/ns1’, ’ns1:someothernode’);<br />

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

$node->appendChild($node2);<br />

$node->appendChild($node3);<br />

$dom->appendChild($node);<br />

echo $dom->saveXML();

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

Saved successfully!

Ooh no, something went wrong!