Zend_Dom gives you a DOMElement ... how to use it?

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) {
    // $result is a DOMElement
}

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)#867 (7) {
  ["_count":protected] => NULL
  ["_cssQuery":protected] => string(8) "div.note"
  ["_document":protected] => object(DOMDocument)#79 (0) {
  }
  ["_nodeList":protected] => object(DOMNodeList)#864 (0) {
  }
  ["_position":protected] => int(0)
  ["_xpath":protected] => NULL
  ["_xpathQuery":protected] => string(33) "//div[contains(@class, ' note ')]"
}

- , :

$children = $newsBlurbNode->childNodes;
     foreach ($children as $child) {
       }

, foreach . Ack! ?

+5
2

- , :

$children = $newsBlurbNode->childNodes;
foreach ($children as $child) {   
    //do something with individual nodes
} 

: http://php.net/manual/en/class.domelement.php

+2

, - - , , - .

$data = "<p id='p_1'><a href='testing1.html'><span>testing in a span 1</span></a></p>
         <p id='p_2'><a href='testing2.html'></a></p>
         <p id='p_3'><a href='testing3.html'><span>testing in a span 3</span></a></p>
         <p id='p_4'><a href='testing4.html'><span>testing in a span 4</span></a></p>
         <p id='p_5'><a href='testing5.html'><span>testing in a span 5</span></a></p>";

$dom = new Zend_Dom_Query();
$dom->setDocumentHtml($data);

//Look for any links inside of paragraph tags
$results = $dom->query('p a');

foreach($results as $r){

   echo "Parent Tag: ".$r->nodeName."<br />";
   echo $r->nodeValue."<br />";
   $children = $r->childNodes;

   if($children->length > 0){

       $children = $r->childNodes;

       foreach($children as $c){
           echo "Child Tag: <br />";
           echo $c->nodeName."<br />";
           echo $c->nodeValue."<br />";
       }

  }

   echo $r->getAttribute('href')."<br /><br />";

}

echo $data;
+2

All Articles