Prev | Current Page 209 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"


But if you want that, it should be exactly under the task node which is just under the
root node, it should be /task/state/assigned/action/notify.
If you need to match any attribute then match it as [@AttributeName1='value'] [@
AttributeName2='value']. If you see the following XPath, it will be clear to you:
//task[@type='analysis']/state[@name='new']/assigned[@to='cto']
DOM API
SimpleXML in PHP is used to parse the document however it cannot create any XML
document. For creating XML documents on the fly you have to use DOM API that
comes bundled with PHP 5. Using DOM API you can also create page-scrapping
tools fairly easily.
In this section we will learn how to create XML documents using DOM API, and
then we will learn how to parse existing documents and modify them.
In the following example we will create just a basic HTML file:
$doc = new DOMDocument("1.0","UTF-8");
$html = $doc->createElement("html");
$body = $doc->createElement("body");
$h1 = $doc->createElement("h1","OOP with PHP");
$body->appendChild($h1);
$html->appendChild($body);
$doc->appendChild($html);
echo $doc->saveHTML();
?>
Chapter 8
[ 205 ]
This will produce the following code:


OOP with PHP




That's fairly easy, right?
Let's do some more:
$doc = new DOMDocument("1.


Pages:
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221