I'm attempting to parse XML and store the element values into an object. The issue I'm running into is that the child elements are repeated so I'm not quite sure the best practice to iterate over it and store the value.
What I was considering doing is looking at a child element and adding a counter. The counter would be used to create an undetermined amount of object containers to store the values. Would this work or is there a better way to do this?
Here is an example of my class:
class SODOCUMENTITEMS:
def __init__(self):
self.recordno = ''
self.dochdrno = ''
self.docid = ''
and here is an example of my XML:
`<sotransitems>
<sotransitem>
<recordno>40562</recordno>
<dochdrno>16987</dochdrno>
<docid/>
<bundlenumber/>
<itemid>13</itemid>
<itemdesc>Winter Lager</itemdesc>
<line_no>0</line_no>
<warehouseid>Main</warehouseid>
<quantity>1</quantity>
<unit>Each</unit>
<price>4.99</price>
<retailprice>4.99</retailprice>
<totalamount>4.99</totalamount>
<taxrate/>
<tax/>
<grossamount/>
<locationid/>
<departmentid/>
<memo/>
<discsurchargememo/>
<revrectemplate/>
<revrecstartdate>
<year></year>
<month></month>
<day></day>
</revrecstartdate>
<revrecenddate>
<year></year>
<month></month>
<day></day>
</revrecenddate>
<renewalmacro/>
<currency>USD</currency>
<exchratedate>
<year></year>
<month></month>
<day></day>
</exchratedate>
<exchratetype/>
<exchrate>1</exchrate>
<trx_price>4.99</trx_price>
<trx_value>4.99</trx_value>
<projectid/>
<customerid>2--2</customerid>
<vendorid/>
<employeeid/>
<classid/>
<contractid/>
<taskno/>
<billingtemplate/>
<sourcedocumentid/>
<sourcedocumentkey/>
<sourcedocumententrytkey/>
<discountpercent/>
<linesubtotals/>
<customfields>
<customfield>
<customfieldname>TESTCUSTOM</customfieldname>
<customfieldvalue>true</customfieldvalue>
</customfield>
<customfield>
<customfieldname>TEST_NUMBER</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>NUMBER1</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>TEST_DATE</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>MYTESTFIELD</customfieldname>
<customfieldvalue>true</customfieldvalue>
</customfield>
<customfield>
<customfieldname>TESTBOX1</customfieldname>
<customfieldvalue>true</customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMUSERDEFINEDDEMTSS</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMA</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMA777</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMAA5678</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMSITE</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
</customfields>
</sotransitem>
<sotransitem>
<recordno>40563</recordno>
<dochdrno>16987</dochdrno>
<docid/>
<bundlenumber/>
<itemid>12</itemid>
<itemdesc>Loktar</itemdesc>
<line_no>1</line_no>
<warehouseid>Main</warehouseid>
<quantity>1</quantity>
<unit>Each</unit>
<price>90</price>
<retailprice>90</retailprice>
<totalamount>90</totalamount>
<taxrate/>
<tax/>
<grossamount/>
<locationid/>
<departmentid>fail</departmentid>
<memo/>
<discsurchargememo/>
<revrectemplate/>
<revrecstartdate>
<year></year>
<month></month>
<day></day>
</revrecstartdate>
<revrecenddate>
<year></year>
<month></month>
<day></day>
</revrecenddate>
<renewalmacro/>
<currency>USD</currency>
<exchratedate>
<year></year>
<month></month>
<day></day>
</exchratedate>
<exchratetype/>
<exchrate>1</exchrate>
<trx_price>90</trx_price>
<trx_value>90</trx_value>
<projectid/>
<customerid>2--2</customerid>
<vendorid/>
<employeeid/>
<classid/>
<contractid/>
<taskno/>
<billingtemplate/>
<sourcedocumentid/>
<sourcedocumentkey/>
<sourcedocumententrytkey/>
<discountpercent/>
<linesubtotals/>
<customfields>
<customfield>
<customfieldname>TESTCUSTOM</customfieldname>
<customfieldvalue>true</customfieldvalue>
</customfield>
<customfield>
<customfieldname>TEST_NUMBER</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>NUMBER1</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>TEST_DATE</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>MYTESTFIELD</customfieldname>
<customfieldvalue>true</customfieldvalue>
</customfield>
<customfield>
<customfieldname>TESTBOX1</customfieldname>
<customfieldvalue>true</customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMUSERDEFINEDDEMTSS</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMA</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMA777</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMAA5678</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
<customfield>
<customfieldname>GLDIMSITE</customfieldname>
<customfieldvalue></customfieldvalue>
</customfield>
</customfields>
</sotransitem>
</sotransitems>`
I'm just looking for a small sample or suggestion on how best handle parsing and storing each set of into a object. Any information will help and I'm find with doing additional research based on your feedback.
Thanks!