[英]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. 没什么复杂的:
wholeText
是DOMText
属性,而不是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
实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.