DOMXPath var_dump: "(object value omitted)" [duplicate]
Asked Answered
M

1

6
$store = curl_exec($ch); // Returns a page of HTML

$doc = new DOMDocument();
$doc->loadHTML($store);
$xpath = new DOMXpath($doc);

Vardump $xpath:

object(DOMXPath)#2 (1) { 
    ["document"] => string(22) "(object value omitted)" 
} 

What is wrong here? I'm trying to use xpath on the HTML code to extract info.

object(DOMDocument)#1 (34) {
    ["doctype"]         => string(22) "(object value omitted)" 
    ["implementation"]  => string(22) "(object value omitted)" 
    ["documentElement"] => string(22) "(object value omitted)" 
    ["actualEncoding"]  => string(6) "gb2312" 
    ["encoding"]        => string(6) "gb2312"
    ["xmlEncoding"]     => string(6) "gb2312" 
    ["standalone"]      => bool(true) 
    ...
Madsen answered 1/7, 2013 at 4:10 Comment(1)
that's not how xpath works... xpath is a search engine for DOM documents, it's NOT the document you loaded.Sparing
F
3

loadHTMLFile requires path of the html file not the content of the html file loadhtmlfile. So your code will be

$doc = new DOMDocument();
$doc->loadHTMLFile("path to html file");
$xpath = new DOMXpath($doc);

Edit

If you want to load from html content use loadhtml

$doc = new DOMDocument();
$doc->loadHTML($store);
$xpath = new DOMXpath($doc);
Frameup answered 1/7, 2013 at 4:17 Comment(6)
Can i load from string?Madsen
$dom->loadHTML($string); ??Madsen
even with loadHTML , i'm still getting the error. I did a echo on $store. It has html in itMadsen
@RainbowHat You will have fetch the result. If you directly var_dump , it will always give you same result.Frameup
As above has ["textContent"]=> string(2304) , it means the html in it. How do i actually Fetch the result correctly? $elements = $xpath->query("//*[@id='detail']/div[1]/h3/text()");Madsen
@RainbowHat I don't know how this query("//*[@id='detail']/div[1]/h3/text()"); works, you can look into documentation.Frameup

© 2022 - 2025 — McMap. All rights reserved.