Xpath选择<a href=""> value

时间:2015-11-13 10:48:35

标签: php xpath

I am trying to select value from a link but, don't know how to solve this problem.

<li id ="listing" class="listing premium first">
   <header>
      <h2>
        <a href="#">value</a>
      </h2>
   </header>
</li>

I tried with this code and didn't work.

echo $test = $xpath->evaluate('//li[contains(@class,"listing")]//header//h2//a')->item(0)->nodeValue;

Here's all of my code:

<?php 
    $getURL = file_get_contents('realestate.com.kh/…'); 
    $dom = new DOMDocument(); 
    @$dom->loadHTML($getURL); 
    $xpath = new DOMXPath($dom); 
    /* echo $xpath->evaluate("normalize-space(substring-before(substring-after(//p[contains(‌​text(),'Property ID:')][1], 'Property ID:'), '–'))");*/ 
    echo $test = $xpath->evaluate('//li[contains(@class,"listing")]//header//h2//a')->item(0)->no‌​deValue;
?>

And here's the actual error I get:

Notice: Trying to get property of non-object in W:\Xampp\htdocs\X\index.php on line 9

Is something wrong with this code? I don't know how to make this work. pls help me anyone. I thanks u all.

0 个答案:

没有答案