0

I have following HTML which I am trying to read from the DOMNodeList when I query a HTML document with xPath in PHP code. This is my query $description = $xpath->query("//div[@class='qsc-html-content']"); I expect that I shall be able to fetch every thing inside 2nd div. Im new to xPath in PHP. The problem is when I try anything like following :

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

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

I do not get any result.

<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>

Some one can please guide me where I am wrong. There is another problem where I am trying to fetch list of anchor tags by xPath query and the result is returning only singe anchor.

I had banged my head onto this for last 2 hours. I am exhausted now.

thanks

1 Answer 1

0

The result from $xpath->query(...) is a DOMNodeList object. It's not a single element, so using:

echo $description->tagName;

is wrong. Instead you should iterate the result, like this:

$description = $xpath->query("//div[@class='qsc-html-content']");
foreach ($description as $item) {
    echo $item->tagName . "\n";
}
4
  • foreach loop didn't output anything. when i check $description->length it shows "0"
    – Wikki
    Apr 25, 2012 at 19:04
  • OK i just tried the exact HTML code you sent and it works as expected. The code foreach ($description as $item) echo $item->tagName; returns "div". So your problem is probably somewhere else.
    – kuba
    Apr 25, 2012 at 19:09
  • Im using tidy class to well format the HTML read from HTML Page on server. Do you think that could be a reason?
    – Wikki
    Apr 25, 2012 at 19:35
  • Yess. when i bypass tidy and directly load the HTML file into DomDocument it worked.
    – Wikki
    Apr 25, 2012 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.