从xml响应中回显特定元素

时间:2013-12-12 12:02:52

标签: php xml foreach

我有一个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服务验证网站http://xmlpitest-ea.dhl.com/serviceval/jsps/main/Main_menu.jsp

的样本跟踪编号和信息

谢谢,

2 个答案:

答案 0 :(得分: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本身就是一个数组,所以你也需要关心它。

答案 1 :(得分: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)
    } 
  }
}