Parse XML using shell/python and store both attributes and data between tags in CSV

I am currently working on a project in which data is structured in an XML . The problem I am having is that some tags may appear or may not appear and, besides the fact that I need to store the data between the element tags, I also have to store the data in the attributes. I need to store this values in a csv file. I am new to XML and also python/shell so this is quite difficult for me. Can you provide some assistance? Here is an example of a file.

<SYSTEM_PrimaryOffer20150430-102>
    <PrimaryOfferList creationDate="2015-04-30" sequenceNumber="102" elementCount="1">
        <PrimaryOffer>
            <offerID>1</offerID>
            <lineup>Internet</lineup>
            <name>Mobile Internet</name>
            <saleStartDate>2015-01-01T00:00:00</saleStartDate>
            <saleEndDate>2016-01-01T00:00:00</saleEndDate>
            <status>Release</status>
            <brand>Mobile</brand>
            <listPriceExcVAT>51.79339</listPriceExcVAT>
            <listPriceIncVAT>45</listPriceIncVAT>
            <contractDurationList>
                <contractDuration>1</contractDuration>
                <!-- May contain one  or more values -->
                <contractDuration>2</contractDuration>
            </contractDurationList>
            <orderTypeList>
                <orderType>Everything</orderType>
                <!-- May contain one  or more values -->
            </orderTypeList>
            <postpaid>1</postpaid>
            <!-- May contain one  or more values -->
            <supplementOffer occurrence="Optional">
                <offerID>12</offerID>
                <displayOrder>700</displayOrder>
            </supplementOffer>
            <supplementOffer occurrence="Mandatory">
                <offerID>13</offerID>
                <displayOrder>800</displayOrder>
            </supplementOffer>
            <supplementOffer occurrence="Conditional">
                <offerID>14</offerID>
                <displayOrder>900</displayOrder>
            </supplementOffer>
            <supplementOffer occurrence="Optional">
                <offerID>15</offerID>
                <displayOrder>810</displayOrder>
            </supplementOffer>
            <offerGroup occurence="Optional">
                <offerID>20</offerID>
                <displayOrder>910</displayOrder>
            </offerGroup>
            <offerGroup occurence="Mandatory">
                <offerID>12</offerID>
                <displayOrder>300</displayOrder>
            </offerGroup>
            <!-- The number of supplement offers and offergroups vary depending on the file - so I need to read them dinamically -->
            <freeunitinfo>MB</freeunitinfo>
            <MBTransfer>11</MBTransfer>
            <MutationDate>2015-04-22T09:02:35</MutationDate>
            <FinancialCode>2222</FinancialCode>
        </PrimaryOffer>
    </PrimaryOfferList>
</SYSTEM_PrimaryOffer20150430-102>


Source: shell

Leave a Reply