I am trying parse this portion of XML and would like it to run in iterative mode on it's own by determining how many times it needs to run.
Also, the line items may or may not have all the values for each column and i am trying to fill these blanks with None, if particular tag/text doesnt exist for any of those, so to map it to right column later in csv conversion
My XML to be parsed (highlighted in bold, invoice line items):
<Invoice>
<DocumentSource>Supplier</DocumentSource>
<DocumentType>Invoice</DocumentType>
.
.
.
<InvoiceLineItems>
<LineItem>
<InvoiceLineNum>1</InvoiceLineNum>
<POLineNum>1</POLineNum>
<Quantity>2</Quantity>
<UOM>EA</UOM>
<UnitPrice>50.00</UnitPrice>
<LineAmount>100.00</LineAmount>
<SalesTaxPercent>9.75</SalesTaxPercent>
<SupplierPartNum />
<ShortDescription>Marley & Me</ShortDescription>
<LongDescription>Marley & Me</LongDescription>
<DeliveryChargeCode/>
</LineItem>
<LineItem>
<InvoiceLineNum>2</InvoiceLineNum>
<LineAmount>-10.00</LineAmount>
</LineItem>
</InvoiceLineItems>
</Invoice>
Output I am expecting need to look something like ...
What I have right now is pretty basic and looks like below:
# Counting Line Items under an Invoice Line Items
for inv_line_items in root.findall('InvoiceLineItems'):
countX = sum([1 for entry in inv_line_items.getiterator('LineItem')])
print(countX)
invoice_ln1 = []
invoice_ln2 = []
for i in range(0, countX):
for z in root[18][i]:
if i == 0:
#invoice_hdr0.append(z.text)
if z.tag == 'InvoiceLineNum':
invoice_ln1.append(z.text)
if z.tag == 'POLineNum':
invoice_ln1.append(z.text)
if z.tag == 'Quantity':
invoice_ln1.append(z.text)
if z.tag == 'UOM':
invoice_ln1.append(z.text)
if z.tag == 'Unit_Price':
invoice_ln1.append(z.text)
if z.tag == 'LineAmount':
invoice_ln1.append(z.text)
if z.tag == 'SalesTaxPercent':
invoice_ln1.append(z.text)
if z.tag == 'SupplierPartNum':
invoice_ln1.append(z.text)
if z.tag == 'ShortDescription':
invoice_ln1.append(z.text)
if z.tag == 'LongDescription':
invoice_ln1.append(z.text)
if z.tag == 'DeliveryChargeCode':
invoice_ln1.append(z.text)
print(invoice_ln1)
else:
#invoice_hdr1.append(z.text)
if z.tag == 'InvoiceLineNum':
invoice_ln2.append(z.text)
if z.tag == 'POLineNum':
invoice_ln2.append(z.text)
if z.tag == 'Quantity':
invoice_ln2.append(z.text)
if z.tag == 'UOM':
invoice_ln2.append(z.text)
if z.tag == 'Unit_Price':
invoice_ln2.append(z.text)
if z.tag == 'LineAmount':
invoice_ln2.append(z.text)
if z.tag == 'SalesTaxPercent':
invoice_ln2.append(z.text)
if z.tag == 'SupplierPartNum':
invoice_ln2.append(z.text)
if z.tag == 'ShortDescription':
invoice_ln2.append(z.text)
if z.tag == 'LongDescription':
invoice_ln2.append(z.text)
if z.tag == 'DeliveryChargeCode':
invoice_ln2.append(z.text)
print(invoice_ln2)