未定义的属性: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?
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;
}
Nothing complicated: wholeText
is a DOMText
property, not a DOMNode
property. 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.
If you want to obtain the text inside the li element, using the convenient method DOMNode::nodeValue
is a good way. Note that this method will also work with a DOMText
instance since the DOMText
class inherits from DOMNode
.