2013-12-12 15 views
0

我對DHL跟蹤有一個xml響應,我想回顯我的php頁面上的特定元素。回覆xml響應中的特定元素

我用下面的代碼打印出來的跟蹤結果沒有格式化:

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服務驗證網站http://xmlpitest-ea.dhl.com/serviceval/jsps/main/Main_menu.jsp

謝謝

回答

1

$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本身將是一個數組,所以你需要關心這一點。

0
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) 
    } 
    } 
} 
+0

肯定會拋出異常,最果然'解析錯誤:語法錯誤,意想不到的T_VARIABLE,期待 '(''還記得'='!='=='&&!='===' – swapnesh

+0

非常感謝你,你的回答和另一個一樣好,我不得不選擇一個作爲最佳答案。解決了我的問題,再次感謝您。 – user2751886