到目前为止,这是我的代码:
$htmlString = file_get_contents("http://developer.adition/rAD41126");
$domDocument = new DOMDocument();
$domDocument->loadHTML($htmlString);
$xpath = new DOMXPATH($domDocument);
$className = "phui-property-list-properties ";
$results = $xpath->query("//dl[@class='$className']/dd[last()]" );
var_dump($results);`
输出是一个空的DOMNodeList
元素。它应该给我html部分的最后一个dd
元素如下所示。 classname "phui-property-list-properties "
有一个有意的额外空间,因为它出现在DOM中。
具有相关元素的DOM部分是
<dl class="phui-property-list-properties ">
<dt class="phui-property-list-key">Project/Package Auditors </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Committed </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Parents </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Branches </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Tags </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Subscribers </dt>
<dd class="phui-property-list-value">
<em>None</em>
</dd>
</dl>
我想获取带有dd
class="phui-property-list-value"
元素
EDIT
即使查询
,我也会得到一个空对象$className = "phui-property-list-properties ";
$results = $xpath->query("//dl[@class='$className']");
var_dump($results);
答案 0 :(得分:2)
DOM对象对var_dump()
和print_r()
不可见。 Read Here
您的代码运行正常并且还显示了结果数,但var_dump()
在此处无效。以下代码显示了如何检查结果。
<?php
$htmlString = <<<EOD
<dl class="phui-property-list-properties ">
<dt class="phui-property-list-key">Project/Package Auditors </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Committed </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Parents </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Branches </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Tags </dt>
<dd class="phui-property-list-value">
<dt class="phui-property-list-key">Subscribers </dt>
<dd class="phui-property-list-value">
<em>None</em>
</dd>
</dl>
EOD;
$domDocument = new DOMDocument();
$domDocument->loadHTML($htmlString);
$xpath = new DOMXPATH($domDocument);
$className = "phui-property-list-properties ";
$results = $xpath->query("//dl[@class='$className']/dd[last()]" );
foreach ($results as $result) {
$xml = $result->ownerDocument->saveXML($result);
var_dump($xml);
}
?>
输出:
string(74) "<dd class="phui-property-list-value">
<em>None</em>
</dd>"