Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取节点时遇到问题';使用php xpath创建文本_Php_Xpath - Fatal编程技术网

获取节点时遇到问题';使用php xpath创建文本

获取节点时遇到问题';使用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 谁能给我指出正确的方向吗 提前感谢, 比尔试试

我有以下html:

<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";
    }
  }
}