Php XML通过xPath获取节点名称
我有这个XMLPhp 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,我希望得到每个子节
<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()