Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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 XML通过xPath获取节点名称_Php_Xml_Xpath - Fatal编程技术网

Php XML通过xPath获取节点名称

Php XML通过xPath获取节点名称,php,xml,xpath,Php,Xml,Xpath,我有这个XML <Parent> <Children> <child1>1</child1> <secondchild>2</secondchild> <child3>3</child3> <fourth>4</fourth> </Children> </Parent> 1. 2. 3. 4. 使用xpath,我希望得到每个子节

我有这个XML

<Parent>
 <Children>
  <child1>1</child1>
  <secondchild>2</secondchild>
  <child3>3</child3>
  <fourth>4</fourth>
 </Children>
</Parent>

1.
2.
3.
4.
使用xpath,我希望得到每个
子节点的节点名,以:

  • 孩子1
  • 第二个孩子
  • 孩子3
  • 第四
类似于
Parent/Children/*[@name]
.. 不针对任何属性,只有主子节点名称可能是:

<?php
$string = '<Parent>
 <Children>
  <child1>1</child1>
  <secondchild>2</secondchild>
  <child3>3</child3>
  <fourth>4</fourth>
 </Children>
</Parent>';

$xml = new SimpleXMLElement($string);

$children = $xml->xpath('/Parent/Children/*');

foreach ($children as $child){
    echo $child->getName() . "\n";
}
xpath('/Parent/Children/*');
foreach($childrenas$child){
echo$child->getName()。“\n”;
}

xpath('/Parent/Children/*');
$result=array();
foreach($childrenas$child){
$result[]=$child->getName();
}
打印(结果);
试试这个XPATH:

Parent/Children/*/name()
这将在结果节点上调用name()函数

Parent/Children/*/name()