如何使用这个 simplexml_load_string 进行 foreach 循环
我正在使用 simplexml_load_string 解析 xml,如下所示:
$xml = simplexml_load_string($response);
echo '{'.
'"Date":"'.$xml->Date[0].'",'.
'"Description":"'.$xml->ShipTos->ShipTo->ShippingGroups->ShippingGroup->OrderItems->OrderItem->Description[0].'",'.
'"Track":"'.$xml->Shipments->Shipment->Track[0].'"'.
'}';
这工作正常,但如果一个节点多次出现在 xml 中,它只会获取一次。有人可以帮助我理解如何专门为描述节点编写 foreach 循环吗?
I am parsing xml using simplexml_load_string like this :
$xml = simplexml_load_string($response);
echo '{'.
'"Date":"'.$xml->Date[0].'",'.
'"Description":"'.$xml->ShipTos->ShipTo->ShippingGroups->ShippingGroup->OrderItems->OrderItem->Description[0].'",'.
'"Track":"'.$xml->Shipments->Shipment->Track[0].'"'.
'}';
This works ok but if a node appears in the xml multiple times it only grabs it once. Can someone please help me understand how I would write a foreach loop specifically for the Description node?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仅引用每个
SimpleXMLObject
的一个实例。例如,
$xml->Date[0]
仅指第一次出现的 Date 对象。要打印所有日期对象,您需要循环遍历它们
或者,您可以使用
children
函数:You are only referring to one instance of each
SimpleXMLObject
.For example
$xml->Date[0]
refers to the first occurence of a Date object only. To print all Date objects you need toloop through them
Alternatively, you could use the
children
function:等等...
and so on...