繁体   English   中英

PHP,DOMXpath。 无效的(x)退货

[英]Php, DOMXpath. Invalid item(x) return

很简单,但是...我们有这样的php代码

$oPath = new \DOMXPath($this->oHtmlProperty);
$oNode = $oPath->query('//div[@class="product-spec__body"]');

foreach ($oNode as $oNodeProperty) {
    $oListTitle = $oPath->query('h2[@class="title title_size_22"]', $oNodeProperty);

    // ### VARIANT 1 (error with message 'Trying to get property of non-object')

    // $aPropertyGroup = [
    //     'title' => $oListTitle->item(0)->textContent,
    //     'property' => []
    // ];

    // ### VARIANT 2

    foreach ($oListTitle as $oListTitleItem){
        $aPropertyGroup = [
             'title' => $oListTitleItem->textContent,
             'property' => []
        ];

        break; // we need only first item
   }

// ....

因此, $oListTitle始终具有->item(0)节点, $oListTitle 当我们尝试获取它时,会出现错误with message 'Trying to get property of non-object'但该节点存在! 当我们做同样的事情但通过迭代(返回相同的节点类,我们称之为-> item(x))时,我们得到了我们所需要的。

有人可以告诉为什么吗? XD

添加:

$ oListTitle是:

object(DOMNodeList)#340 (1) { ["length"]=> int(1) } 

添加:

var_dump($oListTitle->item(0)); 返回这个

object(DOMElement)#338 (18) { ["tagName"]=> string(2) "h2" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "h2" ["nodeValue"]=> string(45) "ОÑновные характериÑтики" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "h2" ["baseURI"]=> NULL ["textContent"]=> string(45) "ОÑновные характериÑтики" } 

另一个词不为空而存在。

我无法使用php 5.6.3 / win32和以下代码(您的代码+一些样板)重现该问题

<?php
$foo = new Foo;
var_export($foo->bar());

class Foo {

    public function __construct() {
        $this->oHtmlProperty = new DOMDocument;
        $this->oHtmlProperty->loadhtml('<html><head><title>...</title></head><body>
    <div class="product-spec__body">
        <h2 class="title title_size_22">h2_1</h2>
        <h2 class="title title_size_22">h2_2</h2>
    </div>
    <div></div>
    <div class="product-spec__body">
        <h2 class="title title_size_22">h2_3</h2>
        <h2 class="title title_size_22">h2_4</h2>
    </div>
</body></html>');
    }

    public function bar() {
        $retval = array(); $aPropertyGroup = array();
        $oPath = new \DOMXPath($this->oHtmlProperty);
        $oNode = $oPath->query('//div[@class="product-spec__body"]');

        foreach ($oNode as $oNodeProperty) {
            $oListTitle = $oPath->query('h2[@class="title title_size_22"]', $oNodeProperty);
            // ### VARIANT 1 (error with message 'Trying to get property of non-object')
            if ( !is_object($oListTitle) ) die('$oListTitle is not an object');
            if ( ! ($oListTitle instanceof DOMNodeList) ) die('$oListTitle is not a DOMNodeList');
            if ( $oListTitle->length < 1 ) die('oListTitle->length < 1');
            $node = $oListTitle->item(0);
            if ( is_null($node) ) die('$node is NULL');
            if ( !is_object($node) ) die('$node is not an object');
            if ( ! ($node instanceof DOMNode) ) die('$node is not a DOMNode');

            $aPropertyGroup = [
                'title' => $oListTitle->item(0)->textContent,
                'property' => []
            ];

            if ( !empty($aPropertyGroup) ) {
                $retval[] = $aPropertyGroup;
                $aPropertyGroup = array();
            }
        } 

        return $retval;
    }
}

输出是

array (
  0 => 
  array (
    'title' => 'h2_1',
    'property' => 
    array (
    ),
  ),
  1 => 
  array (
    'title' => 'h2_3',
    'property' => 
    array (
    ),
  ),
)

如预期的那样。
但是也许libxml_get_last_error()可以告诉您更多...。

您有两个表达式,因此如果第一个匹配项包含多个项目。 内部匹配取决于外部匹配的结果可能会有所不同。 您只需设置一个变量,因此,如果所需结果在外部匹配项之一中,它将填充该变量。

您没有提供HTML,因此无法真正重现该错误。

但是,如果您使用的是DOMNodelist::item() ,则应始终验证返回值是一个节点。

这是两个可能的优化:

  1. 将结果限制为第一个节点:
    h2[@class="title title_size_22"][1]
  2. 以字符串形式获取第一个节点的文本内容(仅与DOMXPath::evaluate() ):
    string(h2[@class="title title_size_22"])

$html = <<<'HTML'
<html><head><title>...</title></head><body>
    <div class="product-spec__body">
        <h2 class="title title_size_22">h2_1</h2>
        <h2 class="title title_size_22">h2_2</h2>
    </div>
    <div></div>
    <div class="product-spec__body">
    </div>
</body></html>
HTML;

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXpath($dom);

foreach ($xpath->evaluate('//div[@class="product-spec__body"]') as $index => $spec) {
  echo "Run #", $index, "\n";
  // all h2 with the class
  var_dump($xpath->evaluate('h2[@class="title title_size_22"]', $spec));
  // first h2 with the class
  var_dump($xpath->evaluate('h2[@class="title title_size_22"][1]', $spec));
  // first h2 with the class as string
  var_dump($xpath->evaluate('string(h2[@class="title title_size_22"])', $spec));
  echo "\n\n";
}

输出-比较两次运行的结果:

Run #0
object(DOMNodeList)#9 (1) {
  ["length"]=>
  int(2)
}
object(DOMNodeList)#8 (1) {
  ["length"]=>
  int(1)
}
string(4) "h2_1"


Run #1
object(DOMNodeList)#8 (1) {
  ["length"]=>
  int(0)
}
object(DOMNodeList)#8 (1) {
  ["length"]=>
  int(0)
}
string(0) ""

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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