简体   繁体   中英

PHP - DOMXpath - Get the result

I have errors when I want to print the result of an evaluate expression with XPath.

$url = $xpath->evaluate('//a/@href', $event); echo $url ;

I have this error : Catchable fatal error: Object of class DOMNodeList could not be converted to string

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.

Quoting 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.

so your XPath returns multiple nodes apparently, which likely stems from you using // which means "find everywhere". If you do echo $url->length; you'll see there is 460 items (no matter the passed context node).

From 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 selects the para element descendants of the context node

So you need to use .//a/@href instead. This will give only 1 result for 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM