2012-04-25 9 views
0

PHPコードでxPathを使用してHTMLドキュメントをクエリすると、次のようなHTMLがDOMNodeListから読み取ろうとしています。これは私のクエリです$description = $xpath->query("//div[@class='qsc-html-content']"); 私は2番目のdiv内のすべてのものを取得することができると期待しています。 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クエリでアンカータグのリストを取得しようとしていて、その結果がsinge anchorだけを返すという別の問題があります。

私はこれに最後の2時間頭を叩いた。私は今、疲れている。 $xpath->query(...)から

おかげ

答えて

0

結果はのDOMNodeListオブジェクトです。それは単一の要素ではないので、使用する:

echo $description->tagName; 

が間違っています。代わりに、次のように結果を反復する必要があります。

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

foreachループで何も出力されませんでした。私は$ description-> lengthをチェックすると "0"と表示されます – Wikki

+0

OKあなたが送信したHTMLコードを試してみました。コード 'foreach($ description as $ item)echo $ item-> tagName;'はdivを返します。あなたの問題はおそらく他の場所にあるでしょう。 – kuba

+0

tidyクラスを使用して、サーバー上のHTMLページから読み取ったHTMLを適切にフォーマットします。それが理由だと思いますか? – Wikki

関連する問題