2012-04-25 104 views
0

當我在PHP代碼中用xPath查詢HTML文檔時,我嘗試從DOMNodeList中讀取以下HTML。這是我的查詢$description = $xpath->query("//div[@class='qsc-html-content']"); 我期望我將能夠獲取第二格內的所有東西。我是PHP中的xPath新手。問題是當我嘗試像下面這樣的東西時:xPath,DOM和NodeList

 if(isset($description) && is_object($description)){ 
     echo "DESCIPTION SET"; 
     echo $description->tagName; 

     //echo $description->getElementsByTagName("p"); 
     $productInfo['description'] = trim($description->nodeValue);    
    } 

我沒有得到任何結果。

<div class="product-description "> 
<div class="qsc-html-content"> 
    <p><span class="Apple-style-span" style="background-color: #ffffff; font-family: Verdana, sans-serif;">Farida is a new and upcoming brand in the hookah market which specializes in solid brass hookahs. The designs are very unique and have not been seen in the hookah industry before. </span>This hookah stand about&nbsp;36".</p> 
    <ul> 
    <li>Farida&nbsp;hose</li> 
    <li>Tongs, Grommets, Bowl, &amp; Farida Gold Tray</li> 
    </ul> 
    <p><span style="color: #ff0000;"><strong><span style="font-size: x-small;">Please Not: </span></strong></span></p> 
    <p>&nbsp;Glass bases are mouth blown and sometimes have air bubbles as evidence of this fact. <span style="font-size: xx-small;">Artisans hand paint each glass base, making every one as unique as the artist. This results in a 100% unique finished product that may not look identical to the photo. It also means that sometimes the paint may have a slight smudge, a line may not be perfectly straight, or you may see the artist's brush strokes.</span></p> 
    <p>Egyptian products are handmade in a traditional manner. Because these hookahs are handmade, there are usually slight variations from hookah to hookah. Most hookahs contain visible weld lines or unpolished metal at the welds.</p> 
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: #ff0000;">FREE STARTER KIT INCLUDES:</span> 1 Holland Charcoal, 10 Mouth Tips, and 2 AL Fakher 50g Tobbacos Select Flavors</p> 
    <p><img alt="" height="55" src="media/round tablets.jpg" width="103" />&nbsp;&nbsp;&nbsp;<img alt="" height="81" src="media/male_mouth_tips.jpg" width="109" /> <img alt="" height="86" src="media/d_al_fakher_50g.jpg" width="126" />&nbsp;&nbsp;&nbsp;&nbsp;</p> 
</div> 

有些人可以請指導我在哪裏,我錯了。還有一個問題,我試圖通過xPath查詢獲取錨點標記列表,並且結果是僅返回單一錨點。

我在最近2個小時裏把我的頭撞到了這裏。我現在筋疲力盡了。

感謝

回答

0

$xpath->query(...)結果是一個的DOMNodeList對象。這不是一個單一的元素,所以使用:

echo $description->tagName; 

是錯誤的。相反,你應該迭代結果,像這樣:

$description = $xpath->query("//div[@class='qsc-html-content']"); 
foreach ($description as $item) { 
    echo $item->tagName . "\n"; 
} 
+0

foreach循環沒有輸出任何東西。當我檢查$ description->長度時顯示「0」 – Wikki 2012-04-25 19:04:23

+0

好吧,我剛剛嘗試了您發送的確切HTML代碼,並且按預期工作。代碼'foreach($ description as $ item)echo $ item-> tagName;'returns「div」。所以你的問題可能在別的地方。 – kuba 2012-04-25 19:09:34

+0

我使用tidy類來很好地格式化從服務器上的HTML頁面讀取的HTML。你認爲這可能是一個原因嗎? – Wikki 2012-04-25 19:35:58