03.02.2014 Views

php|architect's Guide to Web Scraping with PHP - Wind Business ...

php|architect's Guide to Web Scraping with PHP - Wind Business ...

php|architect's Guide to Web Scraping with PHP - Wind Business ...

SHOW MORE
SHOW LESS

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

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

SimpleXML Extension ” 115<br />

echo $sxe->body->ul->li;<br />

// Also outputs "Foo" -- note that "html" is referenced this time<br />

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

$doc->loadHTML($markupString);<br />

$xpath = new DOMXPath($doc);<br />

echo $xpath->evaluate(’/html/body/ul/li[0]’);<br />

// Outputs "ul"<br />

echo $sxe->body->ul->getName(), <strong>PHP</strong>_EOL;<br />

// Outputs "Foo" then "Bar"<br />

foreach ($sxe->body->ul->li as $li) {<br />

echo $li, <strong>PHP</strong>_EOL;<br />

}<br />

// Does the same thing<br />

foreach ($sxe->body->ul->children() as $li) {<br />

echo $li, <strong>PHP</strong>_EOL;<br />

}<br />

?><br />

When referencing a child element of the current element (via the body property of<br />

$sxe above, for example), note that the child element being accessed is also an instance<br />

of SimpleXMLElement. This means it’s not only possible <strong>to</strong> chain element access,<br />

but also <strong>to</strong> reference the last element in such a chain using a variable. See the<br />

first foreach loop shown above for an example of both of these.<br />

Accessing Attributes<br />

Where element access makes use of enumerated arrays for accessing multiple elements<br />

<strong>with</strong> the same name on the same hierarchical level, attribute access makes<br />

use of associative arrays. The example below uses the same $markupString sample<br />

data as in the previous example.<br />

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

Saved successfully!

Ooh no, something went wrong!