2013-03-03 92 views
0

我有我想刮一個表看起來像這樣:麻煩刮表DOMXPath

<table id="thisTable"> 
    <tr> 
     <td class="value1"></td> 
     <td class="value2"></td> 
     <td class="value3"></td> 
     <td class="value4"></td> 
    </tr> 
    <tr> 
     <td class="value5"></td> 
     <td class="value6"></td> 
    </tr> 
</table> 

和我DOMXPath看起來像這樣(到目前爲止):

$htmlDoc = new DomDocument(); 
@$htmlDoc->loadhtml($html); 
$xpath = new DOMXPath($htmlDoc); 

$nodelist = $xpath->query('//*[@id="thisTable"]'); 

foreach ($nodelist as $n){ 
    echo $n->nodeValue."\n"; 
} 

這工作,我得到表的值,但我如何指定nodeValue的類?最終,我的目標是在td的內容value2,value4value5內建立一個新表格。

回答

1
$htmlDoc = new DomDocument(); 
$htmlDoc->loadHTML($html); 
$xpath = new DOMXPath($htmlDoc); 

$nodelist = $xpath->query('//td'); 

foreach ($nodelist as $n){ 
    echo $n->getAttribute("class")."\n"; 
} 

注意:使用getAttribute屬性,如果我理解正確的話,如果你想要的文本內容越來越

0

展開您的XPath查詢:

$class="value1"; 
$nodelist = $xpath->query('//*[@id="thisTable"][@class="$class"]'); 
0

不知道類的值value2,value4和value5在一行中,您可以使用此xpath:

(//td[@class='value2'] | //td[@class='value4'] | //td[@class='value5'])/text() 

例如:

<table id="thisTable"> 
    <tr> 
    <td class="value1"> 1111</td> 
    <td class="value2"> 222 </td> 
    <td class="value3">333 </td> 
    <td class="value4"> 444</td> 
    </tr> 
    <tr> 
    <td class="value5"> 555</td> 
    <td class="value6"> 666</td> 
    </tr> 
</table> 

輸出將是:222 444 555