php Dom XPath 获取自身 html 内容?

phpDom XPath to get self html content?

比方说,在 StudentList.html 里面得到了这个数据


<div class="tableHeader">
<div class="header">
<div class="header-item">Name</div>
<div class="header-item">ID</div>
<div class="header-item">Email</div>
</div>

<div class="studentRow"></div>

然后我执行:

$dom = new DomDocument('1.0', 'UTF-8');
$str = file_get_contents("StudentList.html");
if ($dom->loadHTML(mb_convert_encoding($str, 'HTML-ENTITIES', 'UTF-8')) == true) {
    $xpath = new DomXPath($dom);
    $nodeList = $xpath->query("//div[@class='tableHeader']");
    $node = $nodeList->item(0);
    echo "<p>" . $node->nodeValue . "</p>";     
}

但是上面的方法只是获取里面的值,不是我想要的...

我想要的是在 class 块匹配时获得完整块 html 内容。

所以当我执行 echo

时它应该 return 下面的内容
<div class="tableHeader">
<div class="header">
<div class="header-item">Name</div>
<div class="header-item">ID</div>
<div class="header-item">Email</div>
</div>

有什么办法吗?

完整的解决方案 html 包括自己

$xpath = new DomXPath($dom);
$div = $xpath->query("//div[@class='tableHeader']");
$div = $div->item(0);
$content= $dom->saveXML($div);
echo $content;