如何在DomNodeList中通过xpath遍历子元素?

时间:2013-12-05 20:15:53

标签: php xpath html-parsing domxpath

我有这个PHP脚本:

<?php
libxml_use_internal_errors(true);
/* Createa a new DomDocument object */
$dom = new DomDocument;
$dom_grep = new DomDocument;
/* Load the HTML */
$dom->loadHTMLFile("http://domain.com/catalog/0_1.html");
/* Create a new XPath object */
$xpath = new DomXPath($dom);
/* Query all <table> nodes containing specified class name */
$nodes = $xpath->query("/html/.//table[@class='right']");
/* Set HTTP response header to plain text for debugging output */
header("Content-type: text/plain");

/* How to make Xpath in code below??? */
foreach ($nodes as $i => $node) {
        $child[$i]["title"] = $node->query("//tr[@class='bg3']//h3");
        $child[$i]["href"] = $node->query("a['href=/catalog/details']");

    }
}
?>

但是我在结果中遇到了这个错误: &#34;致命错误:调用未定义的方法DOMElement :: query()&#34;在$ child array

如何在$ nodes中进行另一个xpath查询?

谢谢!

0 个答案:

没有答案