你好,
所以我有以下两个 XML 文件。
档案一:
<?xml version="1.0" encoding="UTF-8"?>
<GetShipmentUpdatesResult>
<Shipments>
<Shipment>
<Container>
<OrderNumber>5108046</OrderNumber>
<ContainerNumber>5108046_1</ContainerNumber>
<CustomerOrderNumber>abcq123</CustomerOrderNumber>
<ShipDate>2015-07-12T12:00:00</ShipDate>
<CarrierName>UPS</CarrierName>
<TrackingNumber>1ZX20520A803682850</TrackingNumber>
<StatusCode>InTransit</StatusCode>
<Events>
<TrackingEvent>
<TimeStamp>2015-06-29T13:53:18</TimeStamp>
<City></City>
<StateOrProvince></StateOrProvince>
<Description>manifested from Warehouse</Description>
<TrackingStatus>Manifest</TrackingStatus>
</TrackingEvent>
<TrackingEvent>
<TimeStamp>2015-06-29T18:47:44</TimeStamp>
<City>Glenwillow</City>
<StateOrProvince>OH</StateOrProvince>
<Description>Status: AF Recorded</Description>
<TrackingStatus>In Transit</TrackingStatus>
</TrackingEvent>
</Events>
</Container>
</Shipment>
<Shipment>
<Container>
<OrderNumber>456789</OrderNumber>
<ContainerNumber>44789</ContainerNumber>
<CustomerOrderNumber>abcq123</CustomerOrderNumber>
<ShipDate>2015-07-03T13:56:27</ShipDate>
<CarrierName>UP2</CarrierName>
<TrackingNumber>1Z4561230020</TrackingNumber>
<StatusCode>IN_TRANSIT</StatusCode>
<Events>
<TrackingEvent>
<TimeStamp>2015-07-03T13:56:27</TimeStamp>
<City>Glenwillow</City>
<StateOrProvince>OH</StateOrProvince>
<Description>manifested from Warehouse</Description>
<TrackingStatus>Manifest</TrackingStatus>
</TrackingEvent>
</Events>
</Container>
</Shipment>
</Shipments>
<MatchingRecords>2</MatchingRecords>
<RequestId></RequestId>
<RecordsRemaining>0</RecordsRemaining>
</GetShipmentUpdatesResult>
文件 B:
<?xml version="1.0" encoding="UTF-8"?>
<getShipmentStatusResponse>
<getShipmentStatusResult>
<outcome>
<result>Success</result>
<error></error>
</outcome>
<shipments>
<shipment>
<orderID>123456</orderID>
<containerNo>CD1863663C</containerNo>
<shipDate>2015-06-29T18:47:44</shipDate>
<carrier>UPS</carrier>
<trackingNumber>1Z4561230001</trackingNumber>
<statusCode>IN_TRANSIT</statusCode>
<statusMessage>In Transit</statusMessage>
<shipmentEvents>
<trackingUpdate>
<timeStamp>2015-06-29T13:53:18</timeStamp>
<city />
<state />
<trackingMessage>Manifest</trackingMessage>
</trackingUpdate>
<trackingUpdate>
<timeStamp>2015-06-29T18:47:44</timeStamp>
<city>Glenwillow</city>
<state>OH</state>
<trackingMessage>Shipped from warehouse</trackingMessage>
</trackingUpdate>
</shipmentEvents>
</shipment>
<shipment>
<orderID>456789</orderID>
<containerNo>44789</containerNo>
<shipDate>2015-07-03T13:56:27</shipDate>
<carrier>UP2</carrier>
<trackingNumber>1Z4561230020</trackingNumber>
<statusCode>IN_TRANSIT</statusCode>
<statusMessage>In Transit</statusMessage>
<shipmentEvents>
<trackingUpdate>
<timeStamp>2015-07-03T13:56:27</timeStamp>
<city>Glenwillow</city>
<state>OH</state>
<trackingMessage>Manifest</trackingMessage>
</trackingUpdate>
</shipmentEvents>
</shipment>
</shipments>
<matchingRecords>2</matchingRecords>
<requestId></requestId>
<remainingRecords>0</remainingRecords>
</getShipmentStatusResult>
</getShipmentStatusResponse>
我基本上需要通读文件 A 并将其更改为看起来像文件 B。现在,我一直在使用 xmltodic 来解析文件 A,但它只会读取顶部元素。看来我必须创建多个 for 循环才能使用 xmltodict 实现这一点。一个循环遍历每个父元素,然后是子元素。
看看elementree,这似乎是一样的。有没有人知道任何其他方法可以做到这一点而不必做多个 for 循环?