Php foreach循环如何使用这个simplexml\u加载\u字符串
我使用simplexml\u load\u字符串解析xml,如下所示:Php foreach循环如何使用这个simplexml\u加载\u字符串,php,xml,simplexml,Php,Xml,Simplexml,我使用simplexml\u load\u字符串解析xml,如下所示: $xml = simplexml_load_string($response); echo '{'. '"Date":"'.$xml->Date[0].'",'. '"Description":"'.$xml->ShipTos->ShipTo->ShippingGroups->ShippingGroup->OrderIte
$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循环吗
$datebuffer;
$count = 0;
foreach($xml->Date as $date){
$count++;
datebuffer .= "Date:".$count ." ".$date;
}
等等
依此类推……您只引用每个SimpleXMLObject的一个实例。 例如,$xml->Date[0]仅引用日期对象的第一次出现。要打印所有日期对象,您需要 循环通过它们
foreach( $xml->Date as $date ){
print (string)$date;
}
或者,您可以使用以下功能:
foreach( $xml->children('Date') as $date ){
print (string)$date;
}
您只引用每个SimpleXMLObject的一个实例。 例如,$xml->Date[0]仅引用日期对象的第一次出现。要打印所有日期对象,您需要 循环通过它们
foreach( $xml->Date as $date ){
print (string)$date;
}
或者,您可以使用以下功能:
foreach( $xml->children('Date') as $date ){
print (string)$date;
}
简单的xml会使事情变得更难处理,您尝试过DOMDocument吗$日期=$doc->getElementsByTagNameDate;对于每个$dates都是$date{}?不,谢谢jeremy,我会调查Simple_xml会使事情变得更难处理,我想你试过DOMDocument吗$日期=$doc->getElementsByTagNameDate;每个$dates作为$date{}?不,谢谢jeremy我会调查的