I'm trying to use Zend_Dom for a very light screen scripting (I want to grab the headline, some body text and a link from a small news block on my site), and I'm not sure how to handle the DOMElement that it gives me.
The manual for Zend_Dom in the code says:
foreach ($results as $result) {
}
How can I use this DOMElement?
Detailed example (search for anchor elements in Google):
$url='http://google.com/';
$client = new Zend_Http_Client($url);
$response = $client->request();
$html = $response->getBody();
$dom = new Zend_Dom_Query($html);
$results = $dom->query('a');
foreach($results as $r){
Zend_Debug::dump($r);
}
This gives me:
object(DOMElement)#81 (0) {
}
object(DOMElement)#82 (0) {
}
object(DOMElement)#83 (0) {
}
... etc, etc...
What I'm confusing is that it looks like every element contains nothing (0)! This is not the case, but this is my first impression. Therefore, I expose myself on the Internet and find that I can add nodeValueto get some of this:
Zend_Debug::dump($r->nodeValue);
which gives me:
string(6) "Images"
string(6) "Videos"
string(4) "Maps"
...etc, etc...
, , .
, html:
<div class="newsBlurb">
<span class="newsDate">Mon, 11 October 2010</span>
<h3 class="newsHeadline"><a href="http://foo.com/1/2/">Some text</a></h3>
<a class="newsMore" href="http://foo.com/1/2/">More</a>
</div>
<div class="hr"></div>
<div class="newsBlurb">
<span class="newsDate">Mon, 16 August 2010</span>
<h3 class="newsHeadline"><a href="http://bar.com/pants.html">Stuff is here</a></h3>
<a class="newsMore" href="http://bar.com/pants.html">More</a>
</div>
newsBlurb, , Google, . -, - . , , div.
, ?
, , . , ?
$url = 'http://php.net/manual/en/class.domelement.php';
$client = new Zend_Http_Client($url);
$response = $client->request();
$html = $response->getBody();
$dom = new Zend_Dom_Query($html);
$newsBlurbNode = $dom->query('div.note');
Zend_Debug::dump($newsBlurbNode);
:
object(Zend_Dom_Query_Result)
["_count":protected] => NULL
["_cssQuery":protected] => string(8) "div.note"
["_document":protected] => object(DOMDocument)
}
["_nodeList":protected] => object(DOMNodeList)
}
["_position":protected] => int(0)
["_xpath":protected] => NULL
["_xpathQuery":protected] => string(33) "//div[contains(@class, ' note ')]"
}
- , :
$children = $newsBlurbNode->childNodes;
foreach ($children as $child) {
}
, foreach . Ack! ?