user1500341
user1500341

Reputation:

How to reach till the desired node in xpath result?

As I have mentioned in question title, I am trying below code to reach till the desired node in xpath result.

<?php
$xpath = '//*[@id="topsection"]/div[3]/div[2]/div[1]/div/div[1]';          
$html = new DOMDocument();
@$html->loadHTMLFile('http://www.flipkart.com/samsung-galaxy-ace-s5830/p/itmdfndpgz4nbuft');
$xml = simplexml_import_dom($html);   
if (!$xml) {
    echo 'Error while parsing the document';
    exit;
}

$source = $xml->xpath($xpath);
echo "<pre>";
print_r($source);
?>

this is the source code. I am using to scrap price from a ecommerce. it works it gives below output :

Array
(
    [0] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [class] => line
                )

            [div] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [class] => prices
                            [itemprop] => offers
                            [itemscope] => 
                            [itemtype] => http://schema.org/Offer
                        )

                    [span] =>  Rs. 10300
                    [div] => (Prices inclusive of taxes)
                    [meta] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [itemprop] => price
                                            [content] => Rs. 10300
                                        )

                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [itemprop] => priceCurrency
                                            [content] => INR
                                        )

                                )

                        )

                )

        )

)

Now How to reach till directly [content] => Rs. 10300. I tried:

echo $source[0]['div']['meta']['@attributes']['content']

but it doesn't work.

Upvotes: 1

Views: 288

Answers (2)

hakre
hakre

Reputation: 198101

The print_r of a SimpleXMLElement does not show the real object structure. So you need to have some knowledge:

$source[0]->div->meta['content']
        |    |     |      `- attribute acccess
        |    |     `- element access, defaults to the first one
        |    `- element access, defaults to the first one
        |
 standard array access to get 
 the first SimpleXMLElement of xpath()
 operation

That example then is (with your address) the following (print_r again, Demo):

SimpleXMLElement Object
(
    [0] => Rs. 10300
)

Cast it to string in case you want the text-value:

$rs = (string) $source[0]->div->meta['content'];

However you can already directly access that node with the xpath expression (if that is a single case).

Learn more on how to access a SimpleXMLElement in the Basic SimpleXML usage ExamplesDocs.

Upvotes: 0

Ranty
Ranty

Reputation: 3362

Try echo (String) $source[0]->div->meta[0]['content'];.

Basically, when you see an element is an object, you can't access it like an array, you need to use object -> approach.

Upvotes: 1

Related Questions