我有一个XML源来跟踪包
<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、起点、目的地
$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">
对于不同的扫描阶段,在我的情况下,我称之为扫描状态。
[仅供参考:这些是快递公司网络中包裹的不同阶段]。
最后我想建立一个数组
$SCANS = array(); // Master Array for all the Scans available
foreach($xml->object->field[6] $scans)
{
$SCAN[] = array(); // Sub Array for each Scan
foreach($scans as $scan_info)
{
$SCAN['scan'] = array($scan_info['name']=>$scan_info);
}
$SCANS[] = $SCAN;
}
所以,输出应该是这样的
{
"scan": {
"employee": "JOEL - O94383",
"location": "Amsterdam",
"updated_on": "17 March, 2019, 03:10",
}
"scan": {
"employee": "ELLEN - 49821",
"location": "Amsterdam"
"updated_on": "18 March, 2019, 22:22",
}
}
每次扫描的当前输出数组如下所示
[
{
"": {
"@attributes": {
"type": "CharField",
"name": "Employee"
},
"0": "JOEL - O94383"
}
},
{
"": {
"@attributes": {
"type": "CharField",
"name": "location_city"
},
"0": "AMSTERDAM"
}
},
{
"": {
"@attributes": {
"type": "DateTimeField",
"name": "updated_on"
},
"0": "17 March, 2019, 03:10"
}
}
]
提前感谢!
即使这不是一个严格的错误,我认为最好在结构中有一个扫描数组而不是几个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"
}]