简体   繁体   English

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

[英]Undefined property: 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. 我收到以下错误: Undefined property: DOMElement::$wholeText"当我循环查询并尝试回显整个text结果时。

$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. $xpath->query返回以下时,我print_r$values从在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; 但是如果我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; 现在,我仔细看了一下,看到echo $values->wholeText; it's trying to echo the DOMElement and not the DOMText. 它试图回显DOMElement而不是DOMText。

How can I reference the DOMText instead of the DOMElement when echoing? 回显时如何引用DOMText而不是DOMElement?

Nothing complicated: wholeText is a DOMText property, not a DOMNode property. 没什么复杂的: wholeTextDOMText属性,而不是DOMNode属性。 When the part //h2/following-sibling::ul/li of your XPath query succeeds and returns a node that isn't a DOMText instance, you naturally obtain this error. 当XPath查询的//h2/following-sibling::ul/li部分成功并返回非DOMText实例的节点时,您自然会得到此错误。

If you want to obtain the text inside the li element, using the convenient method DOMNode::nodeValue is a good way. 如果要获取li元素内的文本,则使用便捷的方法DOMNode::nodeValue是一个好方法。 Note that this method will also work with a DOMText instance since the DOMText class inherits from DOMNode . 请注意,由于DOMText类是从DOMNode继承的,因此该方法也可用于DOMText实例。

I was able to solve this problem by checking the nodeType. 我可以通过检查nodeType来解决此问题。

nodeType 1 is an element, 3 is text and so forth, you can find all the nodeTypes here . nodeType 1是元素,3是文本,依此类推,您可以在此处找到所有的nodeTypes。

if($values->nodeType === 3){
    echo $values->textContent;
}

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

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