从xml响应中回显特定元素


echo specific elements from an xml response

我有一个DHL跟踪的xml响应,我想在我的php页面上回显特定的元素。

我使用以下代码打印出不带格式的跟踪结果:

print_r($response);

xml响应如下所示:

Array
(
    [TrackingResponse] => Array
        (
            [xmlns:req] => http://www.dhl.com
            [xmlns:xsi] => http://www.w3.org/2001/XMLSchema-instance
            [xsi:schemaLocation] => http://www.dhl.com TrackingResponse.xsd
            [Response] => Array
                (
                    [ServiceHeader] => Array
                        (
                            [MessageTime] => 2013-12-12T11:51:05+00:00
                            [MessageReference] => j2xfhcBpCE2yd9gbeC5tjqxIX8xjDpZ1
                            [SiteID] => iraqnova
                        )
                )
            [AWBInfo] => Array
                (
                    [AWBNumber] => 8564385550
                    [Status] => Array
                        (
                            [ActionStatus] => success
                        )
                    [ShipmentInfo] => Array
                        (
                            [OriginServiceArea] => Array
                                (
                                    [ServiceAreaCode] => FRA
                                    [Description] => FRANKFURT - GERMANY
                                )
                            [DestinationServiceArea] => Array
                                (
                                    [ServiceAreaCode] => MLA
                                    [Description] => MALTA - MALTA
                                )
                            [ShipperName] => STANDARD CHARTERED BANK
                            [ShipperAccountNumber] => 144193851
                            [ConsigneeName] => BANK OF VALLETTA P.L.C
                            [ShipmentDate] => 2013-02-14T15:14:00
                            [Pieces] => 1
                            [Weight] => 0.08
                            [WeightUnit] => K
                            [GlobalProductCode] => U
                            [ShipmentDesc] => 1402130018
                            [DlvyNotificationFlag] => Y
                            [Shipper] => Array
                                (
                                    [City] => Frankfurt/Main
                                    [PostalCode] => 60486
                                    [CountryCode] => DE
                                )
                            [Consignee] => Array
                                (
                                    [City] => Santa Venera
                                    [PostalCode] => 9030
                                    [CountryCode] => MT
                                )
                            [ShipperReference] => Array
                                (
                                    [ReferenceID] => Doc
                                )
                        )
                )
        )
)

我弄丢了太多foreach循环来获取[ShipmentInfo]标签中的特定xml标签:

foreach($response as $tag){
echo $tag['ShipmentInfo'];
}

样品跟踪编号和信息,来自DHL XML Service Validation网站http://xmlpitest-ea.dhl.com/serviceval/jsps/main/Main_menu.jsp

谢谢你,

$arr['TrackingResponse']['AWBInfo']['ShipmentInfo']将导致装运信息,然后使用foreach对此进行迭代。

类似-

if(is_array($arr['TrackingResponse']['AWBInfo']['ShipmentInfo'])) {
foreach(is_array($arr['TrackingResponse']['AWBInfo']['ShipmentInfo']) as $shiptagkey=>$shiptagval)
{
echo $shiptagkey, " ", $shiptagval;
}
}

虽然$shiptagval本身将是一个数组,所以您也需要关心这一点。

print_r($response ['TrackingResponse']['AWBInfo']['ShipmentInfo']);

或者如果你不知道路径:(伪代码)

function findAndEcho($obj,$tag) {
  for $obj as $key => $val {
    if $key = $tag {
      print_r($val)
    }
    else if is_array($val) {
      findAndEcho($val,$tag)
    } 
  }
}