这段代码哪里错了DOMDOCUMENT
如果换成previousSibling的话就正常的
$doc = new DOMDocument();
$html = <<<HTML
<html>
<body>
<ul id="list">
<li>Foo</li>
<li>Bar</li>
</ul>
<h2 class = 'test'>heading3</h2>
<h3>heading3</h3>
<ul id="list2">
<li>list2</li>
<li>list2</li>
</ul>
</body>
</html>
HTML;
$doc ->loadHTML($html);
$ul = $doc->getElementsByTagName('*');
foreach($ul as $node) {
if ($node -> hasAttribute('class')) {
foreach($node -> nextSibling ->childNodes as $morenodes) {
print_r($morenodes);
}
}
}
[解决办法]
h2 的 next 是 h3,h3没有child
[解决办法]
看这样式,应该是想要获取所有的ul吧?
$ul = $doc->getElementsByTagName('*');
这样是获取所有的元素的
改成$ul = $doc->getElementsByTagName('ul');
这样就会值获取页面的ul了。。
这样的话,下面的循环操作就没有问题了。