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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵