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.

198 ” XML and Web Services<br />

operable, you can use the former for simplicity and the latter for power on the same<br />

document with minimal effort.<br />

Loading and Saving XML Documents<br />

There are two ways to import documents into a DOM tree; the first is by loading them<br />

from a file:<br />

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

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

Alternatively, you can load a document from a string—which is handy when using<br />

REST Web services:<br />

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

$dom->loadXML($xml);<br />

You, can also import HTML files and strings by calling the<br />

DomDocument::loadHTMLFile() and DomDocument::loadHTML() methods respectively.<br />

Just as simply, you can save XML documents using one of DomDocument::save()<br />

(to a file), DomDocument::saveXML() (to a string), DomDocument::saveHTML() (also<br />

to a string, but saves an HTML document instead of an XML file), and<br />

DomDocument:saveHTMLFile() (to a file in HTML format).<br />

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

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

// Do something with our XML here<br />

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

// Save to file<br />

if ($use_xhtml) {<br />

$dom->save(’library.xml’);<br />

} else {<br />

$dom->saveHTMLFile(’library.xml’);<br />

}<br />

// Output the data

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

Saved successfully!

Ooh no, something went wrong!