我需要从 TH childNODES 获取 childNodes 的 xhtml 数据
<table>some data</table>
<table>
<tr>
<td class="c2">PCI Signal Error (SERR#) Enable</td>
<td>Yes</td>
</tr>
<tr>
<td class="c1">Controller Type 1</td>
<td>CISS</td>
</tr>
<tr>
<td class="c2">bus type</td>
<td>CISS</td>
</tr>
<tr>
<th><a name="systempcibus5">PCI Bus 31</a></th>
<td>Device<a href="#system"></a></td>
</tr>
</table>
下面是最新的尝试,我只想获取上面xml中TD的textContent
这样我就可以建一个mysql语句把数据插入到mySql中
上周我尝试了很多变化。
我收到这个错误。我不会让您厌倦我尝试过的所有各种事情,但我相信这是最接近我想要的。
PHP 通知:试图在第 40 行获取 C:\inetpub\wwwroot\reports\gec\test1.php 中非对象的属性
<?php
libxml_use_internal_errors(true);
$dom = new DomDocument;
$dom->loadHTML($html);
$xpath = new DomXPath($dom);
$nodes = $xpath->query('/html/body/table[2]/tr');
//$nodes = $xpath->query("//tr[contains(concat(' ', @class, ' '), ' head ') ");
//header("Content-type: text/plain");
$node_count=$nodes->length ;
for( $i = 1; $i <= intval($node_count); $i++)
{
$node_td1 = $xpath->query('/html/body/table[2]/tr[$i]/td[1]');
$node_td2 = $xpath->query('/html/body/table[2]/tr[$i]/td[2]');
$result1=$node_td1->textContent;
$result2=$node_td2->textContent;
echo $result1 . "," . $result2 . "<br>";
}
最佳答案
或者,您可以只指出行本身,然后使用 ->tagName
过滤掉它们:
$dom = new DomDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
$xpath = new DomXPath($dom);
$rows = $xpath->query('/html/body/table[2]/tr');
foreach ($rows as $row) {
foreach($row->childNodes as $col) {
if(isset($col->tagName) && $col->tagName != 'th') {
echo $col->textContent . '<br/>';
}
}
echo '<hr/>';
}
或者使用 xpath 来引用每一行:
foreach ($rows as $row) {
$col1 = $xpath->evaluate('string(./td[1])', $row);
$col2 = $xpath->evaluate('string(./td[2])', $row);
echo $col1 . '<br/>';
echo $col2 . '<br/>';
echo '<hr/>';
}
关于php - 无法使用php中的xquery从xhtml获取带有xpath的两个子元素来操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27127566/