[英]PHP - DOMXpath - Get the result
I have errors when I want to print the result of an evaluate expression with XPath. 当我想用XPath打印evaluate表达式的结果时,我有错误。
$url = $xpath->evaluate('//a/@href', $event); $ url = $ xpath-> evaluate('// a / @ href',$ event); echo $url ;
echo $ url;
I have this error : Catchable fatal error: Object of class DOMNodeList could not be converted to string 我有这个错误: 可捕获的致命错误:类DOMNodeList的对象无法转换为字符串
My code : 我的代码:
<?php
// Get the HTML Source Code
$url='http://www.parisbouge.com/events/2012/05/01/';
$source = file_get_contents($url);
// DOM document Creation
$doc = new DOMDocument;
$doc->loadHTML($source);
// DOM XPath Creation
$xpath = new DOMXPath($doc);
// Get all events
$events = $xpath->query('//li[@class="nom"]');
// Count number of events
printf('There is %d events<br />', $events->length);
// List all events
for($i = 0; $i < ($events->length); $i++) {
$event = $events->item($i);
$url = $xpath->evaluate('//a/@href', $event);
$nom = $xpath->evaluate('//a/text()', $event);
$lieu = $xpath->evaluate('../li[@class="lieu"]/a/text()', $event);
echo "Result : $url $nom $lieu <br/>";
}
?>
Try this to get information about nodes. 尝试此操作以获取有关节点的信息。
// List all events
for($i = 0; $i < ($events->length); $i++) {
$event = $events->item($i);
$url = $xpath->evaluate('.//a/@href', $event);
$nom = $xpath->evaluate('.//a/text()', $event);
$lieu = $xpath->evaluate('../li[@class="lieu"]/a/text()', $event);
$result = '';
if ($url->length > 0) {
$result .= $url->item(0)->value;
}
if ($nom->length > 0) {
$result .= $nom->item(0)->wholeText;
}
if ($lieu->length > 0) {
$result .= $lieu->item(0)->wholeText;
}
echo $result . "<br />";
//echo "Result : " . $url->item(0)->value . ' | ' . $nom->item(0)->wholeText . ' | ' . $lieu->item(0)->wholeText . "<br/>";
}
Don't forget add checking if node exists etc. To check if there is any nodes you can check nodeList lenght or suppres errors how "Gordon" suggested. 不要忘记添加检查节点是否存在等。要检查是否有任何节点,您可以检查nodeList lenght或抑制“Gordon”建议的错误。
Quoting http://php.net/manual/de/domxpath.evaluate.php 引用http://php.net/manual/de/domxpath.evaluate.php
Returns a typed result if possible or a DOMNodeList containing all nodes matching the given XPath expression.
如果可能,返回类型化结果或包含与给定XPath表达式匹配的所有节点的DOMNodeList。
so your XPath returns multiple nodes apparently, which likely stems from you using //
which means "find everywhere". 所以你的XPath显然会返回多个节点,这可能源于你使用
//
这意味着“无处不在”。 If you do echo $url->length;
如果你
echo $url->length;
you'll see there is 460 items (no matter the passed context node). 你会看到有460个项目(无论传递的上下文节点)。
From http://www.w3.org/TR/xpath/#path-abbrev 来自http://www.w3.org/TR/xpath/#path-abbrev
//para
selects all the para descendants of the document root and thus selects all para elements in the same document as the context node //para
选择文档根的所有para后代,从而选择与上下文节点相同的文档中的所有para元素 .//para
selects the para element descendants of the context node .//para
选择上下文节点的para元素后代 So you need to use .//a/@href
instead. 所以你需要使用
.//a/@href
代替。 This will give only 1 result for echo $url->length;
这将只给出
echo $url->length;
1个结果echo $url->length;
then but it cannot be returned as a typed result, so you have to change your code to 然后它不能作为输入结果返回,因此您必须将代码更改为
$url = $xpath->evaluate('string(.//a/@href)', $event);
$nom = $xpath->evaluate('string(.//a)', $event);
$lieu = $xpath->evaluate('string(../li[@class="lieu"]/a)', $event);
Also note that you can shorten your DOMDocument
creation and loading to 另请注意,您可以缩短
DOMDocument
创建和加载时间
libxml_use_internal_errors(true);
$doc = new DOMDocument;
$doc->loadHTMLFile('http://www.parisbouge.com/events/2012/05/01/');
The call to libxml_use_internal_errors
will suppress any parsing errors. 对
libxml_use_internal_errors
的调用将抑制任何解析错误。
Try with 试试吧
$url = $xpath->evaluate('string(.//a/@href)', $event); echo $url ;
this will give you the href
of the first a
contained within $event
as a string, instead than a node 这将为您提供
$event
包含的第a
href
作为字符串,而不是节点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.