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

我有一个 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"


对于不同的扫描阶段,在我的情况下,我称之为扫描状态。


[仅供参考:这些是快递公司网络中包裹的不同阶段]。


明月笑刀无情
浏览 143回答 1
1回答

慕妹3242003

即使不是严格的错误,我认为最好有一个扫描数组而不是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 infoforeach($scanObjects as $scanObject){&nbsp; &nbsp; $scan = array() ; // store the current scan info&nbsp; &nbsp; // iterate on all "field" in the current scan stage&nbsp; &nbsp; foreach($scanObject->xpath('field') as $field){&nbsp; &nbsp; &nbsp; &nbsp; $key = (string)$field->xpath('@name')[0] ; // name of the field&nbsp; &nbsp; &nbsp; &nbsp; $value = (string) $field ; // value of the field&nbsp; &nbsp; &nbsp; &nbsp; $scan[ $key ] = $value ; // add the field to the current scan info&nbsp; &nbsp; }&nbsp; &nbsp; $scans[] = $scan ; // add the current scan to the scan list}echo json_encode($scans);输出 :[{&nbsp; &nbsp; "updated_on":"17 March, 2019, 03:10 ",&nbsp; &nbsp; "status":"In-transit",&nbsp; &nbsp; "reason_code_number":"002",&nbsp; &nbsp; "scan_status":"PICKUP",&nbsp; &nbsp; "location_city":"AMSTERDAM",&nbsp; &nbsp; "location_type":"Hub",&nbsp; &nbsp; "city_name":"AMSTERDAM",&nbsp; &nbsp; "Employee":"JOEL - O94383"},{&nbsp; &nbsp; "updated_on":"18 March, 2019, 22:22 ",&nbsp; &nbsp; "status":"Bag scanned at Hub",&nbsp; &nbsp; "reason_code":" - ",&nbsp; &nbsp; "reason_code_number":"003",&nbsp; &nbsp; "scan_status":"IN",&nbsp; &nbsp; "location":"I1H",&nbsp; &nbsp; "location_city":"AMSTERDAM",&nbsp; &nbsp; "location_type":"Hub",&nbsp; &nbsp; "city_name":"AMSTERDAM",&nbsp; &nbsp; "Employee":"ELLEN - 49821"}]
打开App,查看更多内容
随时随地看视频慕课网APP