doublel83422 2010-09-22 23:24
浏览 79
已采纳

DomXML xpath接下来我该怎么办?

I have this code:

$reader = new DOMDocument();
$reader->loadHTML($shell);
$xpath = new DomXPath($reader);
$xpath->registerNamespace('html','http://www.w3.org/1999/xhtml');
$res = $xpath->query('descendant-or-self::*[contains(@class,"content")]');
print_r($res);

$shell is just a variable containing the following html code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <meta http-equiv="Content-Language" content="en-us" />

        <title>Hello World</title>
    </head>

    <body>
        <div class="content">
            Hello World!!
        </div>
    </body>
</html>

If I am correct the xpath query:

descendant-or-self::*[contains(@class,"content")]

is supposed to get the div with the class "content". However when I print the array all I see is an empty object:

DOMNodeList Object
(
)

Does this mean that the query didnt work? Is the DomXPath query language different to the SimpleXML Xpath one, because the query works with SimpleXML?

If it is working how do I view and modify the matched nodes?

  • 写回答

2条回答 默认 最新

  • dqqn32019 2010-09-22 23:34
    关注

    print_r - ing a DOMNodeList (or any of the DOM-classes) doesn't do you much good: they're mostly implemented at C / Libxml2 level, and not natively exposed to PHP. As far as I can tell, this will work, add this after your query, and see if you get results:

    foreach($res as $node){
            var_dump($node->ownerDocument->saveXML($node));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题