开发者

Why is the class DOMText returned for childrens after a DOMNodeList iteration?

开发者 https://www.devze.com 2023-02-19 20:23 出处:网络
$html = \'foo blah <a href=\"\">foo bar</a&开发者_JS百科gt; blah <a href=\"\">foo</a> blah foo\';
$html = 'foo blah <a href="">foo bar</a&开发者_JS百科gt; blah <a href="">foo</a> blah foo';
$dom = new DOMDocument();
$dom->loadHtml($html);

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//text()');
echo get_class($nodes);
foreach($nodes as $node) {
    echo '<br />';
    echo get_class($node) . ':'; //look here
    echo $node->wholeText;
}

Why is the class type DOMText for each $node ?


Your query is asking for text nodes with //text(). :)

DOMText is a specialized DOMNode, which in its turn is the base object for just about every DOM related object (except DOMXPath, I believe, which makes sense)

The object hierarchy for DOMText is actually as followed;

DomNode  
 - DOMCharacterData
   - DomText

You can learn more about this by examining the docs

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号