为了账号安全,请及时绑定邮箱和手机立即绑定

从带有对象的 XML Feed 中解析子对象

从带有对象的 XML Feed 中解析子对象

PHP
明月笑刀无情 2021-11-26 16:32:19
我有一个 XML Feed 来跟踪包裹<xyz_name_of_company-objects version="1.0"><object pk="1" model="awb"><field type="BigIntegerField" name="awb_number">5818537179673</field><field type="CharField" name="origin">AMSTERDAM</field><field type="CharField" name="destination">BERLIN</field><field type="CharField" name="current_location_name">BRUSSELS</field><field type="CharField" name="current_location_code">BRSL</field><field name="scans"><object pk="1" model="scan_stages"><field type="DateTimeField" name="updated_on">17 March, 2019, 03:10 </field><field type="CharField" name="status">In-Transit</field><field type="CharField" name="reason_code_number">002</field><field type="CharField" name="scan_status">PICKUP</field><field type="CharField" name="location_city">AMSTERDAM</field><field type="CharField" name="location_type">Hub</field><field type="CharField" name="city_name">AMSTERDAM</field><field type="CharField" name="Employee">JOEL - O94383</field></object><object pk="2" model="scan_stages"><field type="DateTimeField" name="updated_on">18 March, 2019, 22:22 </field><field type="CharField" name="status">Bag scanned at Hub</field><field type="CharField" name="reason_code"> - </field><field type="CharField" name="reason_code_number">003</field><field type="CharField" name="scan_status">IN</field><field type="CharField" name="location">I1H</field><field type="CharField" name="location_city">AMSTERDAM</field><field type="CharField" name="location_type">Hub</field><field type="CharField" name="city_name">AMSTERDAM</field><field type="CharField" name="Employee">ELLEN - 49821</field></object></field></object></xyz_name_of_company-objects>到目前为止,我一直在使用以下代码行来获取主要数据,例如 awb_number、origin、destination$url = A Get URL with Credentials$xml = simplexml_load_file($url);$awb = $xml->object->field[0];$origin= $xml->object->field[1];$destination = $xml->object->field[3];现在我关心的是获取数据对象 pk="1" 模型="scan_stages"对于不同的扫描阶段,在我的情况下,我称之为扫描状态。[仅供参考:这些是快递公司网络中包裹的不同阶段]。
查看完整描述

1 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

即使不是严格的错误,我认为最好有一个扫描数组而不是scan结构中的几个键。


$object = $xml->xpath("//object[@pk = '1' and @model = 'awb']")[0] ;


// get all the scan stages object

$scanObjects = $object->xpath(".//object[@model = 'scan_stages']");


$scans = array(); // store all the scans info

foreach($scanObjects as $scanObject){

    $scan = array() ; // store the current scan info


    // iterate on all "field" in the current scan stage

    foreach($scanObject->xpath('field') as $field){

        $key = (string)$field->xpath('@name')[0] ; // name of the field

        $value = (string) $field ; // value of the field


        $scan[ $key ] = $value ; // add the field to the current scan info

    }


    $scans[] = $scan ; // add the current scan to the scan list

}


echo json_encode($scans);

输出 :


[{

    "updated_on":"17 March, 2019, 03:10 ",

    "status":"In-transit",

    "reason_code_number":"002",

    "scan_status":"PICKUP",

    "location_city":"AMSTERDAM",

    "location_type":"Hub",

    "city_name":"AMSTERDAM",

    "Employee":"JOEL - O94383"

},{

    "updated_on":"18 March, 2019, 22:22 ",

    "status":"Bag scanned at Hub",

    "reason_code":" - ",

    "reason_code_number":"003",

    "scan_status":"IN",

    "location":"I1H",

    "location_city":"AMSTERDAM",

    "location_type":"Hub",

    "city_name":"AMSTERDAM",

    "Employee":"ELLEN - 49821"

}]


查看完整回答
反对 回复 2021-11-26
  • 1 回答
  • 0 关注
  • 142 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信