As explained at http://framework.zend.com/manual/1.12/en/zend.dom.query.html
Once you have a document, you can use either the query()
or queryXpath()
methods; each method will return a Zend_Dom_Query_Result
object with any matching nodes.
And further
As mentioned previously, Zend_Dom_Query_Result
implements both Iterator
and Countable
, and as such can be used in a foreach()
loop as well as with the count()
function.
The various Iterator methods will return PHP's native DOMElements in this case, which means you can access the Element's text content via it's nodeValue
property.
Assuming there is only one item in the Result object, try
$parse->createImdb(
array(
'rating' => $rating->current()->nodeValue
'year' => $year->current()->nodeValue
)
);
If you feel uncomfortable working with an Iterator, you can use:
to, well, copy the iterator into an array and then use that instead, e.g.
$rating = iterator_to_array($rating);
$year = iterator_to_array($year);
$parse->createImdb(
array(
'rating' => $rating[0]->nodeValue
'year' => $year[0]->nodeValue
)
);
If there is multiple results, iterate them together with a MultipleIterator
$ratingsAndYears = new MultipleIterator();
$ratingsAndYears->attachIterator($rating);
$ratingsAndYears->attachIterator($year);
foreach ($ratingsAndYears as $ratingAndYear) {
$parse = new Application_Model_Parse();
$parse->createImdb(
array(
'rating' => $ratingAndYear[0]->nodeValue
'year' => $ratingAndYear[1]->nodeValue
)
);
On a sidenote, Zend_Dom_Query
is just a thin wrapper around PHP's native DOM extension and if you know XPath, you don't really need the W3 Selector querying it offers and can do it all with DOM alone.