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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?