Php 选择XML中的特定节点(循环)

Php 选择XML中的特定节点(循环),php,xml,Php,Xml,我不能只访问xml的一个节点 <destinos> <destino> <programas> <item> <location>Spain</location> ...more content </item> <item> <location>USA</location>

我不能只访问xml的一个节点

<destinos>
 <destino>
     <programas>
       <item>
        <location>Spain</location>
        ...more content
       </item>
       <item>
        <location>USA</location>
       ...more content
       </item>
     </programas>
 </destino>
 <destino>
     <programas>
       <item>
        <location>France</location>
        ...more content
       </item>
       <item>
        <location>Brasil</location>
       ...more content
       </item>
     </programas>
 </destino>
</destinos>
但我只得到了“program”的前两个“item”或四个元素,但在第二个示例中的xml中出现了错误


感谢您的帮助,我已经找到了解决方案,但还没有找到任何能帮助我解决此问题的方法。

这有点假,因为它“手动”添加到根元素中。主循环使用XPath查找所有
标记,然后只输出内容

header('Content-Type: application/xml');

$xml = simplexml_load_string($string);

echo "<root>";
foreach ($xml->xpath("//item") as $item){
    echo $item->asXML();
}
echo "</root>";
header('Content-Type:application/xml');
$xml=simplexml\u load\u string($string);
回声“;
foreach($xml->xpath(//item)作为$item){
echo$item->asXML();
}
回声“;

您是否尝试过先循环
$xml->destinos->destino
?是的,但我无法获取所有“项目”。这是什么意思?你能分享你尝试过的所有方法吗?我尝试过:$nodes=$xml->xpath(“/destinos/destino/programas/item”);标题('Content-Type:application/xml');PROACH($节点为$NULL){CHECK $NOTION-> ASXML();}所有的项目都必须在根元素下才是有效的XML。如果这有帮助,请考虑将其标记为“应答”。
header('Content-Type: application/xml');

foreach ($xml->destinos->destino->programas as $resultado){
    echo $resultado->asXML();
}

or

$xml = simplexml_load_string($string);


$items = $xml->xpath("//destino/programas");

header('Content-Type: application/xml');

foreach ($items as $personaje) {
   echo $personaje->asXML();
}

header('Content-Type: application/xml');

$xml = simplexml_load_string($string);

echo "<root>";
foreach ($xml->xpath("//item") as $item){
    echo $item->asXML();
}
echo "</root>";