2012-04-25 65 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