获取节点时遇到问题';使用php xpath创建文本
我有以下html:获取节点时遇到问题';使用php xpath创建文本,php,xpath,Php,Xpath,我有以下html: <div class="data"> <p class="info">output</p> <p class="info">output2</p> 它不起作用。我有两个错误: Message: Undefined property: DOMElement::$wholeText Message: Undefined variable: output 谁能给我指出正确的方向吗 提前感谢, 比尔试试
<div class="data">
<p class="info">output</p>
<p class="info">output2</p>
它不起作用。我有两个错误:
Message: Undefined property: DOMElement::$wholeText
Message: Undefined variable: output
谁能给我指出正确的方向吗
提前感谢,
比尔试试这个:
$xpath = new DOMXPath($dom);
$elements = $xpath->query("*/div[@class='data']");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
您能否首先指出“不工作”的含义?您的div没有关闭。使用
DOMXPath::query()
如何?错误消息告诉您一切:没有wholeText
属性。这令人惊讶吗?你读过文档了吗?试试echo$elt[0],$elt[1]
我把它改成$elements=$xpath->query(//div[@class='data']);谢谢,这很有效。
$xpath = new DOMXPath($dom);
$elements = $xpath->query("*/div[@class='data']");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}