开发者

How to get element by ID in php DOMDocument class?

I am trying to parse a HTML file using DOMDocument class in PHP.

The sample HTM开发者_如何学PythonL file is

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title></title>
    </head>
    <body>
        <p id="myparagraph"></p>
    </body>
</html>

and I loaded it using

$document = new DOMDocument();
$document->loadHtmlFile("page.html");

Now I try to get the p element by id this way

print_r($document->getElementById("myparagraph"));

This doesn't work for some reason. So what's wrong with this ?


According to comments in the PHP documentation getElementById doesn't really work that well. What you can do is create your own function similar to this:

function getElementById($id)
{
    $xpath = new DOMXPath($this->domDocument);
    return $xpath->query("//*[@id='$id']")->item(0);
}

With thanks to paradox_haze who posted the information in the comments on the PHP docs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜