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

时间:2017-09-09 21:34:01

标签: php dom xpath

当我遍历查询并尝试回显整个文本结果时,我收到以下错误:Undefined property: DOMElement::$wholeText"

$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>';
}

$xpath->query来自foreach的print_r变量时,$values会返回以下内容。

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
)

但如果我echo $values->nodeValue;它有效,虽然它回显所有数据,我只是试图单独访问整个文本,我不明白为什么会出现这个错误?

更新

现在我仔细看了一下,我发现当我echo $values->wholeText;它试图回显DOMElement而不是DOMText时。

如何在回显时引用DOMText而不是DOMElement?

2 个答案:

答案 0 :(得分:1)

没有什么复杂的:wholeTextDOMText属性,而不是DOMNode属性。当XPath查询的部分//h2/following-sibling::ul/li成功并返回不是DOMText实例的节点时,您自然会收到此错误。

如果要获取li元素中的文本,使用方便的方法DOMNode::nodeValue是一种好方法。请注意,此方法也适用于DOMText实例,因为DOMText类继承自DOMNode

答案 1 :(得分:0)

我能够通过检查nodeType来解决这个问题。

nodeType 1是一个元素,3是文本等等,您可以找到所有nodeTypes here

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