0

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)
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
fmolino
  • 11
  • 1
  • 1
  • 2
  • This may help. Read .csv to pandas dataframe then use this approach. http://stackoverflow.com/questions/18574108/how-do-convert-a-pandas-dataframe-to-xml] – scomes Sep 28 '16 at 13:42
  • I actually used that as my first solution, but it doesn't work because I'm creating my own schema on that example.. In my example, I have a pre-existing schema that I need to map my file into. – fmolino Sep 28 '16 at 18:41

2 Answers2

0

There is no generic library to directly convert your CSV to a required XML. You will need to build the XML using xml functions provided by python or using something like xml.etree API

Here's one sample link

This is a flow in which you can achieve it ->

  1. Read CSV
  2. Create your namespace

  3. Create your parent node <Orders>

  4. Read the CSV. For each line, create a new node <Order> and add elements to it. Add this node as a child to parent <Orders>

  5. Repeat Step 4 tile end of file

Community
  • 1
  • 1
Techidiot
  • 1,921
  • 1
  • 15
  • 28
  • I saw that link before... that example wouldn't solve it because my hierarchies don't really show up in the dataset... i would need a way to create each level every time I reach a certain column. One quick note... this format won't change, so i wouldn't need to dynamically create hierarchies or any of that stuff. – fmolino Sep 28 '16 at 12:59
  • Updated the answer. Again, you just need to build it. – Techidiot Sep 28 '16 at 13:07
0

I actually figured out a SQL solution for this...but it is ridiculously manual and specific for my case. I hope the format still helps someone solve a similar issue.

If you notice from the code below, the XML tags will assume the name of the column if you don't include an alias. You can use the format 'Parent/Child' for hierarchies... Look at the bottom of the query to see multiple embedded hierarchies.

SELECT 
    [OrganizationName]
  ,[OrdNum]
  ,[OrdType]
  ,[OrderTMSStatus]
  ,[FreightTerms]
  ,[IsPrePayment]
  ,[ScheduledEarlyPickup]
  ,[ScheduledEarlyDelivery]
  ,[WeightValue]  'TotalGrossWeight/WeightValue'
  ,[WeightBase] 'TotalGrossWeight/WeightBase'
  ,[WeightValue3]  'TotalNetWeight/WeightValue'
  ,[WeightBase5]  'TotalNetWeight/WeightBase'
  ,[VolumeValue]  'TotalGrossVolume/VolumeValue'
  ,[VolumeBase] 'TotalGrossVolume/VolumeBase'
  ,[VolumeValue9] 'TotalNetVolume/VolumeValue'
  ,[VolumeBase11] 'TotalNetVolume/VolumeBase'
  ,[TotalPieceCount]
  ,[TotalHandlingUnitCount]
  ,[IsInPlanning]
  ,[AreTotalsOverridden]
  ,[CurrencyValue] 'FreightValue/CurrencyValue'
  ,[CurrencyBase] 'FreightValue/CurrencyBase'
  ,[IsHot]
  ,[IsHazmat]
  ,[BillingStatus]
  ,[IntegrationStatus]
  ,[OriginLocNum]
  ,[OrganizationName15] 'OriginLoc/OrganizationName'
  ,[TradingPartnerNum] 'OriginLoc/TradingPartnerNum'
  ,[TradingPartnerType] 'OriginLoc/TradingPartnerType'
  ,[LocNum] 'OriginLoc/LocNum'
  ,[LocationType] 'OriginLoc/LocationType'
  ,[IsActive] 'OriginLoc/IsActive'
  ,[IsBillTo] 'OriginLoc/IsBillTo'
  ,[IsRemitTo] 'OriginLoc/IsRemitTo'
  ,[IsCorporate] 'OriginLoc/IsCorporate'
  ,[AddrName] 'OriginLoc/AddrName'
  ,[Addr1] 'OriginLoc/Addr1'
  ,[CityName] 'OriginLoc/CityName'
  ,[StateCode] 'OriginLoc/StateCode'
  ,[CountryISO2] 'OriginLoc/CountryISO2'
  ,[PostalCode] 'OriginLoc/PostalCode'
  ,[CalendarName] 'OriginLoc/CalendarName'
  ,[CalendarAppointmentName] 'OriginLoc/CalendarAppointmentName'
  ,[AllowsHazmat] 'OriginLoc/AllowsHazmat'
  ,[IsDeliveryAptRequired] 'OriginLoc/IsDeliveryAptRequired'
  ,[IsPickupAptRequired] 'OriginLoc/IsPickupAptRequired'
  ,[DestinationLocNum] 
  ,[OrganizationName16] 'DestinationLoc/OrganizationName'
  ,[TradingPartnerNum17] 'DestinationLoc/TradingPartnerNum'
  ,[TradingPartnerType18] 'DestinationLoc/TradingPartnerType'
  ,[LocNum19] 'DestinationLoc/LocNum'
  ,[LocationType20] 'DestinationLoc/LocationType'
  ,[IsActive21] 'DestinationLoc/IsActive'
  ,[IsBillTo22] 'DestinationLoc/IsBillTo'
  ,[IsRemitTo23] 'DestinationLoc/IsRemitTo'
  ,[IsCorporate24] 'DestinationLoc/IsCorporate'
  ,[AddrName25] 'DestinationLoc/AddrName'
  ,[Addr126] 'DestinationLoc/Addr1'
  ,[CityName27] 'DestinationLoc/CityName'
  ,[StateCode28] 'DestinationLoc/StateCode'
  ,[CountryISO229] 'DestinationLoc/CountryISO2'
  ,[PostalCode30] 'DestinationLoc/PostalCode'
  ,[CalendarName31] 'DestinationLoc/CalendarName'
  ,[CalendarAppointmentName32] 'DestinationLoc/CalendarAppointmentName'
  ,[AllowsHazmat33] 'DestinationLoc/AllowsHazmat'
  ,[IsDeliveryAptRequired34] 'DestinationLoc/IsDeliveryAptRequired'
  ,[IsPickupAptRequired35] 'DestinationLoc/IsPickupAptRequired' 
  ,[OrganizationName36] 'Client/OrganizationName'
  ,[TradingPartnerNum37] 'Client/TradingPartnerNum'
  ,[TradingPartnerName] 'Client/TradingPartnerName'
  ,[TradingPartnerType38] 'Client/TradingPartnerType'
  ,[IsActive39] 'Client/IsActive'
  ,[OrdLineNum] 'OrderLines/OrderLine/OrdLineNum'
  ,[WeightValue40] 'OrderLines/OrderLine/WeightGross/WeightValue'
  ,[WeightBase42] 'OrderLines/OrderLine/WeightGross/WeightBase'
  ,[WeightValue44] 'OrderLines/OrderLine/WeightNet/WeightValue'
  ,[WeightBase46] 'OrderLines/OrderLine/WeightNet/WeightBase'
  ,[VolumeValue48] 'OrderLines/OrderLine/VolumeGross/VolumeValue'
  ,[VolumeBase50] 'OrderLines/OrderLine/VolumeGross/VolumeBase'
  ,[VolumeValue52] 'OrderLines/OrderLine/VolumeNet/VolumeValue'
  ,[VolumeBase54] 'OrderLines/OrderLine/VolumeNet/VolumeBase'
  ,[PieceCount] 'OrderLines/OrderLine/PieceCount'
  ,[HandlingUnitCount] 'OrderLines/OrderLine/HandlingUnitCount'
  ,[IsHazmat56] 'OrderLines/OrderLine/IsHazmat'
 FROM [MyDatabase].[dbo].[OrderSample]
 For XML PATH ('Order')

I run that script inside of Python using the pymssql library

#Create a function that reads SQL Query file and returns the result as a string
def queryReturn(query=sql, connection = conn):
    '''
    This function runs a SQL File and returns the result as a strings
    It takes in a T-SQL file as the first argument
    For example:  
    sql=open('sqlFinal.sql', 'r').read().decode('utf-8')
    xml = queryReturn(sql)
    You can also define which database connection to use
    For example: xml = queryReturn(sql,conn)

    '''
    cursor = connection.cursor()
    cursor.execute(query)
    result= "".join([item[0] for item in cursor.fetchall()])
    return result

#Create the header and tail for the XML
header ='''<?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>\n '''
tail = '''\n    </Orders>
</ns1:OrderData> '''

#Add uom to the opening tags that need it
xml2=xml2.replace('<WeightValue','<WeightValue uom="Lb"')
xml2=xml2.replace('<WeightBase','<WeightBase uom="Lb"')
xml2=xml2.replace('<VolumeValue','<VolumeValue uom="CuFt"')
xml2=xml2.replace('<VolumeBase','<VolumeBase uom="CuFt"')
xml2=xml2.replace('<CurrencyValue','<CurrencyValue uom="USD"')
xml2=xml2.replace('<CurrencyBase','<CurrencyBase uom="USD"')

Then I add the top and bottom parts of the query to the main body:

#create final XML string
final = header+xml2+tail
fmolino
  • 11
  • 1
  • 1
  • 2