Hosen MD Altaf
Hosen MD Altaf

Reputation: 61

How can I convert json data to UBL 2.1 standard xml or normal xml to UBL 2.1 standard xml format using python

I have a project where I am using JSON data to convert XML, and for that, I am using the json2xml package. This is working fine. But I need the UBL 2.1 version standard format XML. I searched but don't find any resource online about that how can I do that in python? A package I see > json_to_ubl_xml_transformer .But don't find any documentation. How can I do that?

This is my current XML.

 <?xml version="1.0" ?>
<Invoice>
    <ProfileID>reporting:1.0</ProfileID>
    <ID>INV004</ID>
    <UUID>fd5a7cc4-2316-49ee-ac07-6f4be4be3731</UUID>
    <IssueDate>2022-08-13</IssueDate>
    <IssueTime>23:46:07</IssueTime>
    <InvoiceTypeCode>388</InvoiceTypeCode>
    <InvoiceTypeCodeName>0101001</InvoiceTypeCodeName>
    <DocumentCurrencyCode>SAR</DocumentCurrencyCode>
    <TaxCurrencyCode>SAR</TaxCurrencyCode>
    <Note/>
    <OrderReference>
        <ID/>
    </OrderReference>
    <ContractDocumentReference>
        <ID/>
    </ContractDocumentReference>
    <AdditionalDocumentReference>
        <UUID>4</UUID>
        <PIH>
            <Attachment>
                <EmbeddedDocumentBinaryObject>ET05jV7roub7D66wOAQ49TQ8mCkyldhmH7B8CV3Rc6g=</EmbeddedDocumentBinaryObject>
            </Attachment>
        </PIH>
        <QR>
            <Attachment>
                <EmbeddedDocumentBinaryObject>5D6ZU7f6nb+s1szmMw46l4NZ7yTy0p1wi0ZUMsdQWBE=</EmbeddedDocumentBinaryObject>
            </Attachment>
        </QR>
    </AdditionalDocumentReference>
    <Signature>
        <ID>urn:oasis:names:specification: ubl:signature:Invoice</ID>
        <SignatureMethod>urn:oasis:names:specification:ubl:dsig:enveloped: xades</SignatureMethod>
    </Signature>
    <AccountingSupplierParty>
        <Party>
            <PartyLegalEntity>
                <RegistrationName>Altaf Miazee</RegistrationName>
            </PartyLegalEntity>
            <PartyIdentification>
                <ID/>
            </PartyIdentification>
            <PartyTaxScheme>
                <CompanyID>300600363600003</CompanyID>
            </PartyTaxScheme>
            <PostalAddress>
                <Country>
                    <IdentificationCode>BD</IdentificationCode>
                </Country>
                <AdditionalStreetName>Altafbari</AdditionalStreetName>
                <StreetName>dhaka</StreetName>
                <BuildingNumber>1233</BuildingNumber>
                <PlotIdentification>1233</PlotIdentification>
                <CityName>Dhaka</CityName>
                <PostalZone>12302</PostalZone>
                <CountrySubentity>Dhaka</CountrySubentity>
                <CitySubdivisionName>miazee</CitySubdivisionName>
            </PostalAddress>
        </Party>
    </AccountingSupplierParty>
    <AccountingCustomerParty>
        <Party>
            <PartyLegalEntity>
                <RegistrationName>Hosen MD Altaf</RegistrationName>
            </PartyLegalEntity>
            <PartyIdentification>
                <ID>398765409876333</ID>
            </PartyIdentification>
            <PartyTaxScheme>
                <CompanyID>398765409876333</CompanyID>
            </PartyTaxScheme>
            <PostalAddress>
                <StreetName>الملك سلمان</StreetName>
                <AdditionalStreetName>الملك سلمان</AdditionalStreetName>
                <BuildingNumber>1234</BuildingNumber>
                <PlotIdentification>1234</PlotIdentification>
                <CityName>dhaka</CityName>
                <PostalZone>12234</PostalZone>
                <CountrySubentity>Dhaka</CountrySubentity>
                <CitySubdivisionName>الملك سلمان</CitySubdivisionName>
                <Country>
                    <IdentificationCode>BD</IdentificationCode>
                </Country>
            </PostalAddress>
        </Party>
    </AccountingCustomerParty>
    <Delivery>
        <ActualDeliveryDate>2022-08-25</ActualDeliveryDate>
        <LatestDeliveryDate/>
    </Delivery>
    <PaymentMeans>
        <PaymentMeansCode>10</PaymentMeansCode>
        <PayeeFinancialAccount>
            <PaymentNote/>
        </PayeeFinancialAccount>
    </PaymentMeans>
    <AllowanceCharge>
        <TaxCategory>
            <ID>S</ID>
            <Percent>0.0</Percent>
            <TaxScheme>
                <ID>VAT</ID>
            </TaxScheme>
        </TaxCategory>
        <ChargeIndicator>False</ChargeIndicator>
        <MultiplierFactorNumeric>52.1</MultiplierFactorNumeric>
        <Amount>0.00</Amount>
        <AmountcurrencyID>SAR</AmountcurrencyID>
        <BaseAmount>164263.68</BaseAmount>
        <BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
    </AllowanceCharge>
    <LegalMonetaryTotal>
        <LineExtensionAmount>164263.68</LineExtensionAmount>
        <LineExtensionAmountCurrencyID>SAR</LineExtensionAmountCurrencyID>
        <AllowanceTotalAmount>0.00</AllowanceTotalAmount>
        <AllowanceTotalAmountcurrencyID>SAR</AllowanceTotalAmountcurrencyID>
        <TaxExclusiveAmount>146664.00</TaxExclusiveAmount>
        <TaxExclusiveAmountcurrencyID>SAR</TaxExclusiveAmountcurrencyID>
        <TaxInclusiveAmount>164263.68</TaxInclusiveAmount>
        <TaxInclusiveAmountcurrencyID>SAR</TaxInclusiveAmountcurrencyID>
        <PrepaidAmount/>
        <PrepaidAmountcurrencyID>SAR</PrepaidAmountcurrencyID>
        <PayableAmount>164263.68</PayableAmount>
        <PayableAmountcurrencyID>SAR</PayableAmountcurrencyID>
    </LegalMonetaryTotal>
    <TaxTotal>
        <TaxAmount>17599.68</TaxAmount>
        <VatAmountCurrency>SAR</VatAmountCurrency>
        <VatAmountInAccountingCurrency>17599.68</VatAmountInAccountingCurrency>
        <CurrencyForVatAmountInAccountingCurrency>SAR</CurrencyForVatAmountInAccountingCurrency>
        <TaxSubtotal>
            <TaxableAmount>146664.0</TaxableAmount>
            <TaxableAmountcurrencyID>SAR</TaxableAmountcurrencyID>
            <TaxCategory>
                <ID>E</ID>
                <Percent>1</Percent>
                <TaxExemptionReasonCode>TYIEWE</TaxExemptionReasonCode>
                <TaxExemptionReason>lksdkskak</TaxExemptionReason>
                <TaxScheme>
                    <ID>10.12</ID>
                </TaxScheme>
            </TaxCategory>
            <TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
        </TaxSubtotal>
        <TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
    </TaxTotal>
    <InvoiceLine>
        <item>
            <ID>1</ID>
            <InvoicedQuantity>12</InvoicedQuantity>
            <InvoicedQuantityUnitCode>nos</InvoicedQuantityUnitCode>
            <LineExtensionAmount>146664.0</LineExtensionAmount>
            <LineExtensionAmountcurrencyID>SAR</LineExtensionAmountcurrencyID>
            <AllowanceCharge>
                <ChargeIndicator>False</ChargeIndicator>
                <MultiplierFactorNumeric>0.0</MultiplierFactorNumeric>
                <Amount>0</Amount>
                <AmountcurrencyID>SAR</AmountcurrencyID>
                <BaseAmount>146664.0</BaseAmount>
                <BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
            </AllowanceCharge>
            <TaxTotal>
                <TaxAmount>17599.68</TaxAmount>
                <TaxAmountcurrencyID>SAR</TaxAmountcurrencyID>
                <RoundingAmount>164263.68</RoundingAmount>
                <RoundingAmountcurrencyID>SAR</RoundingAmountcurrencyID>
            </TaxTotal>
            <Item>
                <Name>altaf</Name>
                <BuyersItemIdentification>
                    <ID/>
                </BuyersItemIdentification>
                <SellersItemIdentification>
                    <ID>ITM9</ID>
                </SellersItemIdentification>
                <StandardItemIdentification>
                    <ID/>
                </StandardItemIdentification>
                <ClassifiedTaxCategory>
                    <ID>2</ID>
                    <Percent>12.00</Percent>
                    <TaxScheme>
                        <ID>VAT</ID>
                    </TaxScheme>
                </ClassifiedTaxCategory>
            </Item>
            <Price>
                <PriceAmount>12222.00</PriceAmount>
                <PriceAmountschemeID>SAR</PriceAmountschemeID>
                <AllowanceCharge>
                    <ChargeIdicator>False</ChargeIdicator>
                    <Amount>0</Amount>
                    <AmountcurrencyID>SAR</AmountcurrencyID>
                    <BaseAmount>164263.68</BaseAmount>
                    <BaseAmountcurrencyID>SAR</BaseAmountcurrencyID>
                </AllowanceCharge>
                <BaseQuantity>12</BaseQuantity>
                <BaseQuantityUnitCode/>
            </Price>
        </item>
    </InvoiceLine>
</Invoice>

I want UBL 2.1 standard XML. Which has cbc,cac and other standard namespace. Something like the example below.

   <?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
   <cbc:ProfileID>reporting:1.0</cbc:ProfileID>
   <cbc:ID>55</cbc:ID>
   <cbc:UUID>dfd6a0d1-2dd1-46d1-9e40-bf8d54b44a93</cbc:UUID>
   <cbc:IssueDate>2022-08-04</cbc:IssueDate>
   <cbc:InvoiceTypeCode name="0200000">388</cbc:InvoiceTypeCode>
   <cbc:DocumentCurrencyCode>SAR</cbc:DocumentCurrencyCode>
   <cbc:TaxCurrencyCode>SAR</cbc:TaxCurrencyCode>
   <cac:AdditionalDocumentReference>
      <cbc:ID>ICV</cbc:ID>
      <cbc:UUID>55</cbc:UUID>
   </cac:AdditionalDocumentReference>
   <cac:AccountingSupplierParty>
      <cac:Party>
         <cac:PartyIdentification>
            <cbc:ID schemeID="CRN">8899298738923</cbc:ID>
         </cac:PartyIdentification>
         <cac:PartyName>
            <cbc:Name>Mango Dealers</cbc:Name>
         </cac:PartyName>
         <cac:PostalAddress>
            <cbc:StreetName>5th street</cbc:StreetName>
            <cbc:BuildingNumber>8585</cbc:BuildingNumber>
            <cbc:PlotIdentification>2222</cbc:PlotIdentification>
            <cbc:CitySubdivisionName>Shay</cbc:CitySubdivisionName>
            <cbc:CityName>Cairo</cbc:CityName>
            <cbc:PostalZone>25235</cbc:PostalZone>
            <cbc:CountrySubentity>Makkah</cbc:CountrySubentity>
            <cac:Country>
               <cbc:IdentificationCode>SA</cbc:IdentificationCode>
            </cac:Country>
         </cac:PostalAddress>
         <cac:PartyTaxScheme>
            <cbc:RegistrationName>Mango Dealers</cbc:RegistrationName>
            <cbc:CompanyID>300123469300003</cbc:CompanyID>
            <cac:TaxScheme>
               <cbc:ID>VAT</cbc:ID>
               <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
            </cac:TaxScheme>
         </cac:PartyTaxScheme>
         <cac:PartyLegalEntity>
            <cbc:RegistrationName>Mango Dealers</cbc:RegistrationName>
         </cac:PartyLegalEntity>
      </cac:Party>
   </cac:AccountingSupplierParty>
   <cac:AccountingCustomerParty>
      <cac:Party />
   </cac:AccountingCustomerParty>
   <cac:PaymentMeans>
      <cbc:PaymentMeansCode>10</cbc:PaymentMeansCode>
   </cac:PaymentMeans>
   <cac:TaxTotal>
      <cbc:TaxAmount currencyID="SAR">9.49</cbc:TaxAmount>
      <cac:TaxSubtotal>
         <cbc:TaxableAmount currencyID="SAR">136.45</cbc:TaxableAmount>
         <cbc:TaxAmount currencyID="SAR">0.00</cbc:TaxAmount>
         <cac:TaxCategory>
            <cbc:ID>Z</cbc:ID>
            <cbc:Percent>0</cbc:Percent>
            <cbc:TaxExemptionReasonCode>VATEX-SA-35</cbc:TaxExemptionReasonCode>
            <cbc:TaxExemptionReason>Medicines and medical equipment</cbc:TaxExemptionReason>
            <cac:TaxScheme>
               <cbc:ID>VAT</cbc:ID>
               <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
            </cac:TaxScheme>
         </cac:TaxCategory>
      </cac:TaxSubtotal>
      <cac:TaxSubtotal>
         <cbc:TaxableAmount currencyID="SAR">63.25</cbc:TaxableAmount>
         <cbc:TaxAmount currencyID="SAR">9.49</cbc:TaxAmount>
         <cac:TaxCategory>
            <cbc:ID>S</cbc:ID>
            <cbc:Percent>15</cbc:Percent>
            <cac:TaxScheme>
               <cbc:ID>VAT</cbc:ID>
               <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
            </cac:TaxScheme>
         </cac:TaxCategory>
      </cac:TaxSubtotal>
   </cac:TaxTotal>
   <cac:TaxTotal>
      <cbc:TaxAmount currencyID="SAR">9.49</cbc:TaxAmount>
   </cac:TaxTotal>
   <cac:LegalMonetaryTotal>
      <cbc:LineExtensionAmount currencyID="SAR">199.70</cbc:LineExtensionAmount>
      <cbc:TaxExclusiveAmount currencyID="SAR">199.70</cbc:TaxExclusiveAmount>
      <cbc:TaxInclusiveAmount currencyID="SAR">209.19</cbc:TaxInclusiveAmount>
      <cbc:PayableAmount currencyID="SAR">0</cbc:PayableAmount>
   </cac:LegalMonetaryTotal>
   <cac:InvoiceLine>
      <cbc:ID>1</cbc:ID>
      <cbc:InvoicedQuantity unitCode="PCE">20</cbc:InvoicedQuantity>
      <cbc:LineExtensionAmount currencyID="SAR">136.45</cbc:LineExtensionAmount>
      <cac:AllowanceCharge>
         <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
         <cbc:AllowanceChargeReason>Discount</cbc:AllowanceChargeReason>
         <cbc:Amount currencyID="SAR">2.55</cbc:Amount>
      </cac:AllowanceCharge>
      <cac:TaxTotal>
         <cbc:TaxAmount currencyID="SAR">0.00</cbc:TaxAmount>
         <cbc:RoundingAmount currencyID="SAR">136.45</cbc:RoundingAmount>
      </cac:TaxTotal>
      <cac:Item>
         <cbc:Name>Panadol</cbc:Name>
         <cac:ClassifiedTaxCategory>
            <cbc:ID>Z</cbc:ID>
            <cbc:Percent>0</cbc:Percent>
            <cac:TaxScheme>
               <cbc:ID>VAT</cbc:ID>
               <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
            </cac:TaxScheme>
         </cac:ClassifiedTaxCategory>
      </cac:Item>
      <cac:Price>
         <cbc:PriceAmount currencyID="SAR">6.95</cbc:PriceAmount>
      </cac:Price>
   </cac:InvoiceLine>
   <cac:InvoiceLine>
      <cbc:ID>2</cbc:ID>
      <cbc:InvoicedQuantity unitCode="PCE">4</cbc:InvoicedQuantity>
      <cbc:LineExtensionAmount currencyID="SAR">63.25</cbc:LineExtensionAmount>
      <cac:AllowanceCharge>
         <cbc:ChargeIndicator>false</cbc:ChargeIndicator>
         <cbc:AllowanceChargeReason>Discount</cbc:AllowanceChargeReason>
         <cbc:Amount currencyID="SAR">4.55</cbc:Amount>
      </cac:AllowanceCharge>
      <cac:TaxTotal>
         <cbc:TaxAmount currencyID="SAR">9.49</cbc:TaxAmount>
         <cbc:RoundingAmount currencyID="SAR">72.74</cbc:RoundingAmount>
      </cac:TaxTotal>
      <cac:Item>
         <cbc:Name>Shampoo</cbc:Name>
         <cac:ClassifiedTaxCategory>
            <cbc:ID>S</cbc:ID>
            <cbc:Percent>15</cbc:Percent>
            <cac:TaxScheme>
               <cbc:ID>VAT</cbc:ID>
               <cbc:TaxTypeCode>VAT</cbc:TaxTypeCode>
            </cac:TaxScheme>
         </cac:ClassifiedTaxCategory>
      </cac:Item>
      <cac:Price>
         <cbc:PriceAmount currencyID="SAR">16.95</cbc:PriceAmount>
      </cac:Price>
   </cac:InvoiceLine>
</Invoice>

Here is my json file which generating XML:

{
   "ProfileID":"reporting:1.0",
   "ID":"INV004",
   "UUID":"fd5a7cc4-2316-49ee-ac07-6f4be4be3731",
   "IssueDate":"2022-08-13",
   "IssueTime":"23:46:07",
   "InvoiceTypeCode":388,
   "InvoiceTypeCodeName":"0101001",
   "DocumentCurrencyCode":"SAR",
   "TaxCurrencyCode":"SAR",
   "Note":"",
   "OrderReference":{
      "ID":""
   },
   "ContractDocumentReference":{
      "ID":""
   },
   "AdditionalDocumentReference":{
      "UUID":"4",
      "PIH":{
         "Attachment":{
            "EmbeddedDocumentBinaryObject":"ET05jV7roub7D66wOAQ49TQ8mCkyldhmH7B8CV3Rc6g="
         }
      },
      "QR":{
         "Attachment":{
            "EmbeddedDocumentBinaryObject":"5D6ZU7f6nb+s1szmMw46l4NZ7yTy0p1wi0ZUMsdQWBE="
         }
      }
   },
   "Signature":{
      "ID":"urn:oasis:names:specification: ubl:signature:Invoice",
      "SignatureMethod":"urn:oasis:names:specification:ubl:dsig:enveloped: xades"
   },
   "AccountingSupplierParty":{
      "Party":{
         "PartyLegalEntity":{
            "RegistrationName":"Altaf Miazee"
         },
         "PartyIdentification":{
            "ID":""
         },
         "PartyTaxScheme":{
            "CompanyID":"300600363600003"
         },
         "PostalAddress":{
            "Country":{
               "IdentificationCode":"BD"
            },
            "AdditionalStreetName":"Altafbari",
            "StreetName":"dhaka",
            "BuildingNumber":"1233",
            "PlotIdentification":"1233",
            "CityName":"Dhaka",
            "PostalZone":"12302",
            "CountrySubentity":"Dhaka",
            "CitySubdivisionName":"miazee"
         }
      }
   },
   "AccountingCustomerParty":{
      "Party":{
         "PartyLegalEntity":{
            "RegistrationName":"Hosen MD Altaf"
         },
         "PartyIdentification":{
            "ID":"398765409876333"
         },
         "PartyTaxScheme":{
            "CompanyID":"398765409876333"
         },
         "PostalAddress":{
            "StreetName":"الملك سلمان",
            "AdditionalStreetName":"الملك سلمان",
            "BuildingNumber":"1234",
            "PlotIdentification":"1234",
            "CityName":"dhaka",
            "PostalZone":"12234",
            "CountrySubentity":"Dhaka",
            "CitySubdivisionName":"الملك سلمان",
            "Country":{
               "IdentificationCode":"BD"
            }
         }
      }
   },
   "Delivery":{
      "ActualDeliveryDate":"2022-08-25",
      "LatestDeliveryDate":""
   },
   "PaymentMeans":{
      "PaymentMeansCode":"10",
      "PayeeFinancialAccount":{
         "PaymentNote":""
      }
   },
   "AllowanceCharge":{
      "TaxCategory":{
         "ID":"S",
         "Percent":0.0,
         "TaxScheme":{
            "ID":"VAT"
         }
      },
      "ChargeIndicator":"False",
      "MultiplierFactorNumeric":52.1,
      "Amount":"0.00",
      "AmountcurrencyID":"SAR",
      "BaseAmount":164263.68,
      "BaseAmountcurrencyID":"SAR"
   },
   "LegalMonetaryTotal":{
      "LineExtensionAmount":"164263.68",
      "LineExtensionAmountCurrencyID":"SAR",
      "AllowanceTotalAmount":"0.00",
      "AllowanceTotalAmountcurrencyID":"SAR",
      "TaxExclusiveAmount":"146664.00",
      "TaxExclusiveAmountcurrencyID":"SAR",
      "TaxInclusiveAmount":"164263.68",
      "TaxInclusiveAmountcurrencyID":"SAR",
      "PrepaidAmount":"",
      "PrepaidAmountcurrencyID":"SAR",
      "PayableAmount":"164263.68",
      "PayableAmountcurrencyID":"SAR"
   },
   "TaxTotal":{
      "TaxAmount":"17599.68",
      "VatAmountCurrency":"SAR",
      "VatAmountInAccountingCurrency":"17599.68",
      "CurrencyForVatAmountInAccountingCurrency":"SAR",
      "TaxSubtotal":{
         "TaxableAmount":146664.0,
         "TaxableAmountcurrencyID":"SAR",
         "TaxCategory":{
            "ID":"E",
            "Percent":1,
            "TaxExemptionReasonCode":"TYIEWE",
            "TaxExemptionReason":"lksdkskak",
            "TaxScheme":{
               "ID":10.12
            }
         },
         "TaxAmountcurrencyID":"SAR"
      },
      "TaxAmountcurrencyID":"SAR"
   },
   "InvoiceLine":[
      {
         "ID":1,
         "InvoicedQuantity":"12",
         "InvoicedQuantityUnitCode":"nos",
         "LineExtensionAmount":146664.0,
         "LineExtensionAmountcurrencyID":"SAR",
         "AllowanceCharge":{
            "ChargeIndicator":"False",
            "MultiplierFactorNumeric":0.0,
            "Amount":"0",
            "AmountcurrencyID":"SAR",
            "BaseAmount":146664.0,
            "BaseAmountcurrencyID":"SAR"
         },
         "TaxTotal":{
            "TaxAmount":"17599.68",
            "TaxAmountcurrencyID":"SAR",
            "RoundingAmount":"164263.68",
            "RoundingAmountcurrencyID":"SAR"
         },
         "Item":{
            "Name":"altaf",
            "BuyersItemIdentification":{
               "ID":""
            },
            "SellersItemIdentification":{
               "ID":"ITM9"
            },
            "StandardItemIdentification":{
               "ID":""
            },
            "ClassifiedTaxCategory":{
               "ID":2,
               "Percent":"12.00",
               "TaxScheme":{
                  "ID":"VAT"
               }
            }
         },
         "Price":{
            "PriceAmount":"12222.00",
            "PriceAmountschemeID":"SAR",
            "AllowanceCharge":{
               "ChargeIdicator":false,
               "Amount":"0",
               "AmountcurrencyID":"SAR",
               "BaseAmount":"164263.68",
               "BaseAmountcurrencyID":"SAR"
            },
            "BaseQuantity":"12",
            "BaseQuantityUnitCode":""
         }
      }
   ]
}

Here is my python function which is generating xml from json .Using json2xml package.

def download_invoice_xml(request, invoice_type, invoice_no):
    in_data = InvoiceReport.objects.filter(invoice_type=invoice_type, invoice_number=invoice_no)
    for inv in in_data:
        jsonData = inv.invoice_data
        mode = inv.creation_mode  
    if jsonData.get('AllowanceCharge').get('ChargeIndicator') == 0:
        jsonData['AllowanceCharge']['ChargeIndicator'] = False
    for i in jsonData.get('InvoiceLine'):
        if i.get('AllowanceCharge').get('ChargeIndicator') == 0:
            i['AllowanceCharge']['ChargeIndicator'] = False
        if i.get('Price').get('AllowanceCharge').get('ChargeIndicator') == 0:
            i['Price']['AllowanceCharge']['ChargeIndicator'] = False
    xml_output = json2xml.Json2xml(jsonData, wrapper="Invoice", pretty=True, attr_type=False).to_xml()
    now = datetime.now()
    if xml_output:
        response = HttpResponse(xml_output, content_type='application/xml')
        xml_date = jsonData['IssueDate'].replace("-", "")
        xml_time = jsonData['IssueTime'].replace(":", "")
        filename = jsonData['AccountingSupplierParty']['Party']['PartyTaxScheme']['CompanyID'] + "_" + \
                   xml_date + "T" + xml_time + "_" + str(jsonData['ID']) + ".xml"  
        content = "attachment; filename=%s" % (filename)
        response['Content-Disposition'] = content
        return response
    return HttpResponse("Not found")

Upvotes: 0

Views: 2312

Answers (0)

Related Questions