dongmei5168 2017-09-09 21:34
浏览 81
已采纳

未定义的属性:DOMElement :: $ wholeText

I'm getting the following error: Undefined property: DOMElement::$wholeText" when I loop over the query and try to echo the wholeText result.

$try = $xpath->query('//h2/following-sibling::ul/li | //h2/following-sibling::h3/text()');

foreach($try as $values){
    echo $values->wholeText;    

    //echo '<pre>', print_r($values), '</pre>';
}

The $xpath->query returns the following when I print_r the $values variable from the foreach.

DOMText Object {
    [wholeText] => House Data
    [data] => House Data
    [length] => 10
    [nodeName] => #text
    [nodeValue] => House Data
    [nodeType] => 3
    [parentNode] => (object value omitted)
    [childNodes] => 
    [firstChild] => 
    [lastChild] => 
    [previousSibling] => 
    [nextSibling] => 
    [attributes] => 
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => 
    [baseURI] => 
    [textContent] => House Data
)

DOMElement Object (
    [tagName] => li
    [schemaTypeInfo] => 
    [nodeName] => li
    [nodeValue] => 12 doors are very expensive
    [nodeType] => 1
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => 
    [nextSibling] => (object value omitted)
    [attributes] => (object value omitted)
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => li
    [baseURI] => 
    [textContent] => 12 doors are very expensive
)

DOMText Object (
    [wholeText] => Car Data
    [data] => Car Data
    [length] => 8
    [nodeName] => #text
    [nodeValue] => Car Data
    [nodeType] => 3
    [parentNode] => (object value omitted)
    [childNodes] => 
    [firstChild] => 
    [lastChild] => 
    [previousSibling] => 
    [nextSibling] => 
    [attributes] => 
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => 
    [baseURI] => 
    [textContent] => Car Data
)

DOMElement Object (
    [tagName] => li
    [schemaTypeInfo] => 
    [nodeName] => li
    [nodeValue] => The car has 4 doors
    [nodeType] => 1
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => 
    [nextSibling] => (object value omitted)
    [attributes] => (object value omitted)
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => li
    [baseURI] => 
    [textContent] => The car has 4 doors
)

But if I echo $values->nodeValue; it works, although it echo's all of the data, I'm just trying to access the wholeText individually, I don't understand why this error is popping up?

UPDATE:

Now that I took a closer look, I see that when I echo $values->wholeText; it's trying to echo the DOMElement and not the DOMText.

How can I reference the DOMText instead of the DOMElement when echoing?

  • 写回答

2条回答 默认 最新

  • dongsang6899 2017-09-09 22:02
    关注

    I was able to solve this problem by checking the nodeType.

    nodeType 1 is an element, 3 is text and so forth, you can find all the nodeTypes here.

    if($values->nodeType === 3){
        echo $values->textContent;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序