title: Zend_Dom gives you a DOMElement... how do I use it?
body: <p>I'm trying to use Zend_Dom for some very light screen scraping (I want to grab a headline, some body text and a link from a small block of news items on my website) and I'm not sure how to handle the DOMElement that it gives me.</p>
  
  <p>In the manual for Zend_Dom the code says:</p>
  
  <pre><code>foreach ($results as $result) {
      // $result is a DOMElement
  }
  </code></pre>
  
  <p>How do I make use of this DOMElement?</p>
  
  <p>A detailed example (looking for the anchor elements on Google):</p>
  
  <pre><code>$url='http://google.com/';
  $client = new Zend_Http_Client($url);
  $response = $client-&gt;request();
  $html = $response-&gt;getBody();
  $dom = new Zend_Dom_Query($html);
  $results = $dom-&gt;query('a');
  foreach($results as $r){
       Zend_Debug::dump($r);
  }
  </code></pre>
  
  <p>This gives me:</p>
  
  <pre><code>object(DOMElement)#81 (0) {
  }
  object(DOMElement)#82 (0) {
  }
  object(DOMElement)#83 (0) {
  }
  ... etc, etc...
  </code></pre>
  
  <p>What I find confusing is that this looks like each element contains nothing (0)! This isn't the case but that is my first impression. So I poke around online and find I can add <code>nodeValue</code> to get something out of this:</p>
  
  <pre><code>Zend_Debug::dump($r-&gt;nodeValue);
  </code></pre>
  
  <p>which gives me:</p>
  
  <pre><code>string(6) "Images"
  string(6) "Videos"
  string(4) "Maps"
  ...etc, etc...
  </code></pre>
  
  <p>But where I run into trouble is getting specific elements and their contents.</p>
  
  <p>For instance given this html:</p>
  
  <pre><code>  &lt;div class="newsBlurb"&gt;
     &lt;span class="newsDate"&gt;Mon, 11 October 2010&lt;/span&gt;
     &lt;h3 class="newsHeadline"&gt;&lt;a href="http://foo.com/1/2/"&gt;Some text&lt;/a&gt;&lt;/h3&gt;
     &lt;a class="newsMore" href="http://foo.com/1/2/"&gt;More&lt;/a&gt;
    &lt;/div&gt; 
    &lt;div class="hr"&gt;&lt;/div&gt;
    &lt;div class="newsBlurb"&gt;
     &lt;span class="newsDate"&gt;Mon, 16 August 2010&lt;/span&gt;
     &lt;h3 class="newsHeadline"&gt;&lt;a href="http://bar.com/pants.html"&gt;Stuff is here&lt;/a&gt;&lt;/h3&gt;
     &lt;a class="newsMore" href="http://bar.com/pants.html"&gt;More&lt;/a&gt;
    &lt;/div&gt; 
  </code></pre>
  
  <p>I can grab the text from each newsBlurb, using the technique I use in the Google example, but cannot get each element by itself. I want to get the date and stick it somewhere, get the headline text and stick it somewhere and get the link to use. But all I get is the actual text in the div.</p>
  
  <p>How do I get what I want from this?</p>
  
  <hr>
  
  <p><strong>EDIT</strong>
  Here is another example that does not work as I expect. Any ideas why?</p>
  
  <pre><code>$url = 'http://php.net/manual/en/class.domelement.php';
  $client = new Zend_Http_Client($url);
  $response = $client-&gt;request();
  $html = $response-&gt;getBody();
  $dom = new Zend_Dom_Query($html);
  $newsBlurbNode = $dom-&gt;query('div.note');
  Zend_Debug::dump($newsBlurbNode);
  </code></pre>
  
  <p>this gives me:</p>
  
  <pre><code>object(Zend_Dom_Query_Result)#867 (7) {
    ["_count":protected] =&gt; NULL
    ["_cssQuery":protected] =&gt; string(8) "div.note"
    ["_document":protected] =&gt; object(DOMDocument)#79 (0) {
    }
    ["_nodeList":protected] =&gt; object(DOMNodeList)#864 (0) {
    }
    ["_position":protected] =&gt; int(0)
    ["_xpath":protected] =&gt; NULL
    ["_xpathQuery":protected] =&gt; string(33) "//div[contains(@class, ' note ')]"
  }
  </code></pre>
  
  <p>Trying to get anything out of this I used:</p>
  
  <pre><code>$children = $newsBlurbNode-&gt;childNodes;
       foreach ($children as $child) {
         }
  </code></pre>
  
  <p>Which results in an error because the foreach loop has nothing in it. Ack! What am I not getting?</p>