クエリをループして全体テキストの結果をエコーバックしようとすると、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のから$values
変数をprint_r
する場合は、次を返します。
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;
場合、それはエコーデータのすべてのですが、このエラーがポップアップされた理由を、私は個別にwholeTextにアクセスしようとしている、私は理解していませんか?
UPDATE:
今、私は近い見ていたことを、私はecho $values->wholeText;
たときにDOMElementのではなくDOMTEXTをエコーしようとしていることがわかります。
エコー時にDOMElementではなくDOMTextを参照するにはどうすればよいですか?
クエリの最初の部分に 'text()'を入れないのはなぜですか? '// h2/following-sibling :: ul/li/text()' – raina77ow
@ raina77ow返信ありがとうございます。 h3のテキストが必要なだけなので、私はそれを行うことはできません。liのデータには、必要なhtmlハイパーリンクタグがあります。 – Craig
'(wholeText'の代わりに)' textContent'をチェックするのはどうですか? XMLの構造が何であるかははっきりしていません.DOMElement-> firstChildに行くのに十分かどうかは分かりません。 – raina77ow