我有这个示例代码,它将提取每个标记的值。 除此之外,获取该标签的类名..
<?php
$doc = new DOMDocument;
$doc->loadxml( <<< eox
<tr class="calendar_row" data-eventid="42023">
<td class="date"/>
<td class="time">All Day</td>
<td class="currency">CAD</td>
<td class="impact">
<span title="Non-Economic" class="holiday"/>
</td>
<td class="event">
<span>Bank Holiday</span>
</td>
<td class="detail">
<a class="calendar_detail level1" data-level="1" title="Open Detail"/>
</td>
<td class="actual"/>
<td class="forecast"/>
<td class="previous"/>
<td class="graph"/>
</tr>
eox
);
$xpath = new DOMXPath($doc);
foreach( $xpath->query('//tr[@data-eventid="42023"]/td[@class]') as $n ) {
echo $n->nodeName.'-'.$n->nodeValue."<br />";
}
?>
使用上面的代码片段,我想要的就是获取这些值,即使某些标签格式不正确(我正在废弃网络资源)。我如何在DOMDocument XPath查询中执行此操作。我遇到了麻烦,因为要获取的值是:
td-
td-All Day
td-CAD
td-
td-Bank Holiday
td-
td-
td-
td-
td-
而不是:
date-
time-All Day
currency-CAD
impact-
event-Bank Holiday
detail-
actual-
forecast-
previous-
graph-
答案 0 :(得分:3)
而不是$n->nodeName
你应该这样做$n->getAttribute('class')
。
答案 1 :(得分:1)
echo $n->getAttribute("class") . '-' . $n->nodeValue . "<br />";