I've seen a couple of answers of how to convert a csv (or SQL table) to XML, but I haven't seen one that includes hierarchies and isn't overtly complicated. I need to map a csv file into a pre-existing XML format to feed it to an API using Python. I can already send a valid XML to the website, but I'm having issues converting the csv to the XML in the first place.
My CSV has this format:
OrganizationName,OrdNum,OrdType,OrderTMSStatus,FreightTerms,IsPrePayment,ScheduledEarlyPickup,ScheduledEarlyDelivery,WeightValue,uom,WeightBase,uom2,WeightValue3,uom4,WeightBase5,uom6,VolumeValue,uom7,VolumeBase,uom8,VolumeValue9,uom10,VolumeBase11,uom12,TotalPieceCount,TotalHandlingUnitCount,IsInPlanning,AreTotalsOverridden,CurrencyValue,uom13,CurrencyBase,uom14,IsHot,IsHazmat,BillingStatus,IntegrationStatus,OriginLocNum,OrganizationName15,TradingPartnerNum,TradingPartnerType,LocNum,LocationType,IsActive,IsBillTo,IsRemitTo,IsCorporate,AddrName,Addr1,CityName,StateCode,CountryISO2,PostalCode,CalendarName,CalendarAppointmentName,AllowsHazmat,IsDeliveryAptRequired,IsPickupAptRequired,DestinationLocNum,OrganizationName16,TradingPartnerNum17,TradingPartnerType18,LocNum19,LocationType20,IsActive21,IsBillTo22,IsRemitTo23,IsCorporate24,AddrName25,Addr126,CityName27,StateCode28,CountryISO229,PostalCode30,CalendarName31,CalendarAppointmentName32,AllowsHazmat33,IsDeliveryAptRequired34,IsPickupAptRequired35,OrganizationName36,TradingPartnerNum37,TradingPartnerName,TradingPartnerType38,IsActive39,OrdLineNum,WeightValue40,uom41,WeightBase42,uom43,WeightValue44,uom45,WeightBase46,uom47,VolumeValue48,uom49,VolumeBase50,uom51,VolumeValue52,uom53,VolumeBase54,uom55,PieceCount,HandlingUnitCount,IsHazmat56
My-Organization,PythonTest1,Planning,New,PPD,FALSE,3/17/2016 13:30,3/21/2016 20:00,30000,Lb,30000,Lb,30000,Lb,30000,Lb,2100,CuFt,2100,CuFt,2100,CuFt,2100,CuFt,2100,26,FALSE,FALSE,0,USD,0,USD,FALSE,FALSE,New,New,DC_OH,My-Organization,Test,Client,DC_OH,ShipReceive,TRUE,FALSE,FALSE,FALSE,DC_OH,--,Hamilton,OH,US,45014,Mon-Fri-8-5,24/7 Appointment,FALSE,FALSE,FALSE,CZ_906,My-Organization,Test,Client,CZ_906,ShipReceive,TRUE,FALSE,FALSE,FALSE,7-ELEVEN CDC C/O GENESIS LOGISTICS,--,Santa Fe Springs,CA,US,90670,Mon-Fri-8-5,24/7 Appointment,FALSE,FALSE,FALSE,My-Organization,Test,Test,Client,TRUE,1,30000,Lb,30000,Lb,30000,Lb,30000,Lb,2100,CuFt,2100,CuFt,2100,CuFt,2100,CuFt,1170,26,FALSE
My-Organization,PythonTest2,Planning,New,PPD,FALSE,3/16/2016 14:00,3/21/2016 21:00,25000,Lb,25000,Lb,25000,Lb,25000,Lb,2300,CuFt,2300,CuFt,2300,CuFt,2300,CuFt,2300,26,FALSE,FALSE,0,USD,0,USD,FALSE,FALSE,New,New,DC_KY,My-Organization,Test,Client,DC_KY,ShipReceive,TRUE,FALSE,FALSE,FALSE,DC_KY,--,Florence,KY,US,41042,Mon-Fri-8-5,24/7 Appointment,FALSE,FALSE,FALSE,CZ_906,My-Organization,Test,Client,CZ_906,ShipReceive,TRUE,FALSE,FALSE,FALSE,7-ELEVEN CDC C/O GENESIS LOGISTICS,--,Santa Fe Springs,CA,US,90670,Mon-Fri-8-5,24/7 Appointment,FALSE,FALSE,FALSE,My-Organization,Test,Test,Client,TRUE,1,25000,Lb,25000,Lb,25000,Lb,25000,Lb,2300,CuFt,2300,CuFt,2300,CuFt,2300,CuFt,1170,26,FALSE
This is how it should look:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:OrderData xmlns:ns1="http://schemas.3gtms.com/tms/v1/tns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Orders>
<Order>
<OrganizationName>My-Organization</OrganizationName>
<OrdNum>PythonTest1</OrdNum>
<OrdType>Planning</OrdType>
<OrderTMSStatus>New</OrderTMSStatus>
<FreightTerms>PPD</FreightTerms>
<IsPrePayment>false</IsPrePayment>
<ScheduledEarlyPickup>2016-03-17T13:30:00.000</ScheduledEarlyPickup>
<ScheduledEarlyDelivery>2016-03-21T20:00:00.000</ScheduledEarlyDelivery>
<TotalGrossWeight>
<WeightValue uom="Lb">30000</WeightValue>
<WeightBase uom="Lb">30000</WeightBase>
</TotalGrossWeight>
<TotalNetWeight>
<WeightValue uom="Lb">30000</WeightValue>
<WeightBase uom="Lb">30000</WeightBase>
</TotalNetWeight>
<TotalGrossVolume>
<VolumeValue uom="CuFt">2100</VolumeValue>
<VolumeBase uom="CuFt">2100</VolumeBase>
</TotalGrossVolume>
<TotalNetVolume>
<VolumeValue uom="CuFt">2100</VolumeValue>
<VolumeBase uom="CuFt">2100</VolumeBase>
</TotalNetVolume>
<TotalPieceCount>2100</TotalPieceCount>
<TotalHandlingUnitCount>26</TotalHandlingUnitCount>
<IsInPlanning>false</IsInPlanning>
<AreTotalsOverridden>false</AreTotalsOverridden>
<FreightValue>
<CurrencyValue uom="USD">0</CurrencyValue>
<CurrencyBase uom="USD">0</CurrencyBase>
</FreightValue>
<IsHot>false</IsHot>
<IsHazmat>false</IsHazmat>
<BillingStatus>New</BillingStatus>
<IntegrationStatus>New</IntegrationStatus>
<OriginLocNum>DC_OH</OriginLocNum>
<OriginLoc>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerType>Client</TradingPartnerType>
<LocNum>DC_OH</LocNum>
<LocationType>ShipReceive</LocationType>
<IsActive>true</IsActive>
<IsBillTo>false</IsBillTo>
<IsRemitTo>false</IsRemitTo>
<IsCorporate>false</IsCorporate>
<AddrName>DC_OH</AddrName>
<Addr1>--</Addr1>
<CityName>Hamilton</CityName>
<StateCode>OH</StateCode>
<CountryISO2>US</CountryISO2>
<PostalCode>45014</PostalCode>
<CalendarName>Mon-Fri-8-5</CalendarName>
<CalendarAppointmentName>24/7 Appointment</CalendarAppointmentName>
<AllowsHazmat>false</AllowsHazmat>
<IsDeliveryAptRequired>false</IsDeliveryAptRequired>
<IsPickupAptRequired>false</IsPickupAptRequired>
</OriginLoc>
<DestinationLocNum>CZ_906</DestinationLocNum>
<DestinationLoc>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerType>Client</TradingPartnerType>
<LocNum>CZ_906</LocNum>
<LocationType>ShipReceive</LocationType>
<IsActive>true</IsActive>
<IsBillTo>false</IsBillTo>
<IsRemitTo>false</IsRemitTo>
<IsCorporate>false</IsCorporate>
<AddrName>7-ELEVEN CDC C/O GENESIS LOGISTICS</AddrName>
<Addr1>--</Addr1>
<CityName>Santa Fe Springs</CityName>
<StateCode>CA</StateCode>
<CountryISO2>US</CountryISO2>
<PostalCode>90670</PostalCode>
<CalendarName>Mon-Fri-8-5</CalendarName>
<CalendarAppointmentName>24/7 Appointment</CalendarAppointmentName>
<AllowsHazmat>false</AllowsHazmat>
<IsDeliveryAptRequired>false</IsDeliveryAptRequired>
<IsPickupAptRequired>false</IsPickupAptRequired>
</DestinationLoc>
<Client>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerName>Test</TradingPartnerName>
<TradingPartnerType>Client</TradingPartnerType>
<IsActive>true</IsActive>
</Client>
<OrderLines>
<OrderLine>
<OrdLineNum>1</OrdLineNum>
<WeightGross>
<WeightValue uom="Lb">30000</WeightValue>
<WeightBase uom="Lb">30000</WeightBase>
</WeightGross>
<WeightNet>
<WeightValue uom="Lb">30000</WeightValue>
<WeightBase uom="Lb">30000</WeightBase>
</WeightNet>
<VolumeGross>
<VolumeValue uom="CuFt">2100</VolumeValue>
<VolumeBase uom="CuFt">2100</VolumeBase>
</VolumeGross>
<VolumeNet>
<VolumeValue uom="CuFt">2100</VolumeValue>
<VolumeBase uom="CuFt">2100</VolumeBase>
</VolumeNet>
<PieceCount>1170</PieceCount>
<HandlingUnitCount>26</HandlingUnitCount>
<IsHazmat>false</IsHazmat>
</OrderLine>
</OrderLines>
</Order>
<Order>
<OrganizationName>My-Organization</OrganizationName>
<OrdNum>PythonTest2</OrdNum>
<OrdType>Planning</OrdType>
<OrderTMSStatus>New</OrderTMSStatus>
<FreightTerms>PPD</FreightTerms>
<IsPrePayment>false</IsPrePayment>
<ScheduledEarlyPickup>2016-03-16T14:00:00.000</ScheduledEarlyPickup>
<ScheduledEarlyDelivery>2016-03-21T21:00:00.000</ScheduledEarlyDelivery>
<TotalGrossWeight>
<WeightValue uom="Lb">25000</WeightValue>
<WeightBase uom="Lb">25000</WeightBase>
</TotalGrossWeight>
<TotalNetWeight>
<WeightValue uom="Lb">25000</WeightValue>
<WeightBase uom="Lb">25000</WeightBase>
</TotalNetWeight>
<TotalGrossVolume>
<VolumeValue uom="CuFt">2300</VolumeValue>
<VolumeBase uom="CuFt">2300</VolumeBase>
</TotalGrossVolume>
<TotalNetVolume>
<VolumeValue uom="CuFt">2300</VolumeValue>
<VolumeBase uom="CuFt">2300</VolumeBase>
</TotalNetVolume>
<TotalPieceCount>2300</TotalPieceCount>
<TotalHandlingUnitCount>26</TotalHandlingUnitCount>
<IsInPlanning>false</IsInPlanning>
<AreTotalsOverridden>false</AreTotalsOverridden>
<FreightValue>
<CurrencyValue uom="USD">0</CurrencyValue>
<CurrencyBase uom="USD">0</CurrencyBase>
</FreightValue>
<IsHot>false</IsHot>
<IsHazmat>false</IsHazmat>
<BillingStatus>New</BillingStatus>
<IntegrationStatus>New</IntegrationStatus>
<OriginLocNum>DC_KY</OriginLocNum>
<OriginLoc>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerType>Client</TradingPartnerType>
<LocNum>DC_KY</LocNum>
<LocationType>ShipReceive</LocationType>
<IsActive>true</IsActive>
<IsBillTo>false</IsBillTo>
<IsRemitTo>false</IsRemitTo>
<IsCorporate>false</IsCorporate>
<AddrName>DC_KY</AddrName>
<Addr1>--</Addr1>
<CityName>Florence</CityName>
<StateCode>KY</StateCode>
<CountryISO2>US</CountryISO2>
<PostalCode>41042</PostalCode>
<CalendarName>Mon-Fri-8-5</CalendarName>
<CalendarAppointmentName>24/7 Appointment</CalendarAppointmentName>
<AllowsHazmat>false</AllowsHazmat>
<IsDeliveryAptRequired>false</IsDeliveryAptRequired>
<IsPickupAptRequired>false</IsPickupAptRequired>
</OriginLoc>
<DestinationLocNum>CZ_906</DestinationLocNum>
<DestinationLoc>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerType>Client</TradingPartnerType>
<LocNum>CZ_906</LocNum>
<LocationType>ShipReceive</LocationType>
<IsActive>true</IsActive>
<IsBillTo>false</IsBillTo>
<IsRemitTo>false</IsRemitTo>
<IsCorporate>false</IsCorporate>
<AddrName>7-ELEVEN CDC C/O GENESIS LOGISTICS</AddrName>
<Addr1>--</Addr1>
<CityName>Santa Fe Springs</CityName>
<StateCode>CA</StateCode>
<CountryISO2>US</CountryISO2>
<PostalCode>90670</PostalCode>
<CalendarName>Mon-Fri-8-5</CalendarName>
<CalendarAppointmentName>24/7 Appointment</CalendarAppointmentName>
<AllowsHazmat>false</AllowsHazmat>
<IsDeliveryAptRequired>false</IsDeliveryAptRequired>
<IsPickupAptRequired>false</IsPickupAptRequired>
</DestinationLoc>
<Client>
<OrganizationName>My-Organization</OrganizationName>
<TradingPartnerNum>Test</TradingPartnerNum>
<TradingPartnerName>Test</TradingPartnerName>
<TradingPartnerType>Client</TradingPartnerType>
<IsActive>true</IsActive>
</Client>
<OrderLines>
<OrderLine>
<OrdLineNum>1</OrdLineNum>
<WeightGross>
<WeightValue uom="Lb">25000</WeightValue>
<WeightBase uom="Lb">25000</WeightBase>
</WeightGross>
<WeightNet>
<WeightValue uom="Lb">25000</WeightValue>
<WeightBase uom="Lb">25000</WeightBase>
</WeightNet>
<VolumeGross>
<VolumeValue uom="CuFt">2300</VolumeValue>
<VolumeBase uom="CuFt">2300</VolumeBase>
</VolumeGross>
<VolumeNet>
<VolumeValue uom="CuFt">2300</VolumeValue>
<VolumeBase uom="CuFt">2300</VolumeBase>
</VolumeNet>
<PieceCount>1170</PieceCount>
<HandlingUnitCount>26</HandlingUnitCount>
<IsHazmat>false</IsHazmat>
</OrderLine>
</OrderLines>
</Order>
</Orders>
Help is much appreciated.
In case it is helpful for anyone trying to do something similar, this is how you upload the valid XML to the system:
import requests
filename = 'somefile.xml'
api_url = 'someurl.com'
headers = {'Content-Type': 'application/xml'}
response = requests.post(api_url, data=open(filename).read(), headers=headers)