How do I print the 开发者_开发问答an attribute of an element?
example:
$doc = new DOMDocument();
@$doc->loadHTML($page);
$xpath = new DOMXPath($doc);
$arts= $xpath->query("/td");
foreach ($arts as $art) {
// here i wanna print the attribute class of the td element, how do i do so ?
}
use DOMElement::getAttribute
$art->getAttribute('class');
also, simpleHTMLDOM is more suitable for dealing with html:
$html = str_get_html($page);
foreach($html->find('td') as $element)
echo $element->class.'<br>';
}
DOMXPath's query function returns a DOMNodeList, which (I'm pretty sure) cannot be used in a [Edit: it can]. foreach($ARRAY) loopYou'll have to implement a modified [Edit: not necessary; see below]for loop in order to read the DOMNode elements inside the list class:
foreach ($arts as $art) {
# code-hardiness checking
if ($art && $art->hasAttributes()) {
# (note: chaining will only work in PHP 5+)
$class = $art->attributes->getNamedItem('class');
print($class . "\n");
}
}
加载中,请稍侯......
精彩评论