0

I have an issue with some XML files. I cannot say a lot about data, because it is for work and I don't want to be in trouble! From a huge XML file, 123091 lines of code, I only need data from 7 tags(if that makes sense). I am trying to extract that specific data, but I am having a bit of a situation when trying to store into pandas or csv. I have found a method to take some information out, like:

for info in root.iter('ArtistName'):
   print(info.text)

The code above will give me the artists in the data from that XML tag. Here is a little part of my Jupyter Notebook, with the output of the above lines of code:

Various Artists
Various Artists
Various Artists
Various Artists
Various Artists
Cream
Various Artists
Various Artists
Various Artists
Various Artists
Various Artists
Fleetwood Mac
Fleetwood Mac
Linkin Park
Lynyrd Skynyrd
Fleetwood Mac
Eric Clapton
The Black Keys
Tegan And Sara

And then, I have run into the problem, because in the below code, I cannot or better said, I don't know how to loop over each tag from XML to extract the data. Below is an attempt:

import xml.etree.ElementTree as ET
import pandas as pd

tree = ET.parse("filename.xml")
root = tree.getroot()
dfcols = ['IRC', 'IRC2', 'Artist', 'Song', 'Units', 'PPD', 'TerritoryCode']
df_xml = pd.DataFrame(columns = dfcols)

for i in root.iter(tree):
   df_xml = df_xml.append(pd.Series(index=dfcols), ignore_index=True)

df_xml.head()

The result of the above code is:

 IRC IRC2 Artist Song Units PPD TerritoryCode

Which is the header of the file that I want to create. I cannot find a way to bring the information I need into these columns.

I have also tried this:

def getValOfNode(node):
    return node.text if node is not None else None


def main():

    dfcols = ['IRC', 'IRC2', 'Artist', 'Song', 'Units', 'PPD', 'TerritoryCode']
    df_xml = pd.DataFrame(columns = dfcols)

    for node in tree:
        IRC = node.find('IRC')
        IRC2 = node.find('ICPN')
        Artist = node.find('rtistName')
        Song = node.find('Title')
        Units = node.find('ConsumerSales')
        PPD = node.find('Amount')
        TerritoryCode = node.find('TerritoryCode')

        df_xml = df_xml.append(
            pd.Series([getValOfNode(IRC), getValOfNode(IRC2), getValOfNode(Artist), getValOfNode(Song), getValOfNode(Units), getValOfNode(PPD), getValOfNode(TerritoryCode)], index=dfcols), ignore_index=True)

    print(df_xml)


main()

And I get this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-1f816143f9e4> in <module>()
     23 
     24 
---> 25 main()

<ipython-input-5-1f816143f9e4> in main()
      8     df_xml = pd.DataFrame(columns = dfcols)
      9 
---> 10     for node in tree:
     11         IRC = node.find('IRC')
     12         IRC2 = node.find('ICPN')

TypeError: 'ElementTree' object is not iterable

There is also an issue with the territory code, when I run:

for info in root.iter('TerritoryCode'):
   print(info.text)

it prints the territories but, in order, because they are duplicates(I don't know how to explain), I really need all of them and not just one of each. If that makes sense. This is what I get:

AE
AR
AT
AU
AW
BE
BG
BO
BR
BY
CA
CH
CL
CN
CO
CR
CY
CZ
DE
DK
DO
DZ
EC
EE
EG
ES
FI
FR
GB
GL
GR
GT
HK
HN

This is what I need:

AD
AD
AE
AE
AE
AE
AE
AE,

and so forth.

Can anyone help me with this? Much appreciated.

Have a great day :)

I Russu
  • 5
  • 1
  • 6
  • Can you provide at least a dummy example of XML as we need to see structure to help? By the way: [Never call DataFrame.append or pd.concat inside a for-loop. It leads to quadratic copying.](https://stackoverflow.com/a/36489724/1422451) – Parfait Nov 22 '18 at 18:47
  • Hi @Parfait, I will provide this on Monday as I am back at work. Thank you :) – I Russu Nov 25 '18 at 01:16
  • @Parfait , I have edited the post with XML code (it is just one part of the file), the entire file looks like this. I said what I need - in the code, hope it helps! :) Thank you – I Russu Nov 26 '18 at 08:46
  • Thanks for being so nice, and here is a link https://gist.github.com/ruioana/99b666f5192f47d0f016bb9154ca536b , I hope it makes sense now. – I Russu Nov 27 '18 at 15:26
  • Thanks for post. However, your XML is not valid as the namespace prefix in root *dsr* is not defined. – Parfait Nov 27 '18 at 15:30
  • I have just posted a comment on https://gist.github.com/ruioana/99b666f5192f47d0f016bb9154ca536b , is this still not valid? – I Russu Nov 27 '18 at 15:37
  • Yes that is a correct root opening. BTW - you can revise your own Github gist. This is why it is always advised to post at least the full root due to namespaces denoted by the special attribute, *xmlns*. – Parfait Nov 27 '18 at 20:37

1 Answers1

0

As mentioned, your needed nodes are at different levels of the XML and hence path expressions will be different for each data item. Additionally you need to traverse between two repeating levels: SalesToRecordCompanyByTerritory and ReleaseTransactionsToRecordCompany.

Therefore, consider parsing in nested for loops. And rather than growing a data frame inside a loop, build a list of dictionaries that you can pass into pandas' DataFrame() constructor outside of the loop. With this approach, you migrate dictionary keys as columns and elements as data.

Below uses chained find() calls, long relative, or short absolute paths to navigate down the nested levels and retrieve corresponding element text values. Notice all parsing are relative to looped nodes with parent terr and child rls objects.

import xml.etree.ElementTree as ET
import pandas as pd

tree = ET.parse("file.xml")

data = []
for terr in tree.findall('.//SalesToRecordCompanyByTerritory'):

    for rls in terr.findall('.//ReleaseTransactionsToRecordCompany'):

        inner = {}

        # DESCENDANTS
        inner['IRC'] = rls.find('./ReleaseId/ISRC').text    
        inner['IRC2'] = rls.find('./ReleaseId/ICPN').text

        # CHILDREN
        inner['Artist'] = rls.find('WMGArtistName').text
        inner['Song'] = rls.find('WMGTitle').text

        # DESCENDANTS
        inner['Units'] = rls.find('./SalesTransactionToRecordCompany/SalesDataToRecordCompany/GrossNumberOfConsumerSales').text    
        inner['PPD'] = rls.find('Deal').find('AmountPayableInCurrencyOfAccounting').text

        # PARENT
        inner['TerritoryCode'] = terr.find('./TerritoryCode').text

        data.append(inner)

df = pd.DataFrame(data)

You can shorten the find() chains and long relative paths with absolute paths using .//:

inner['IRC'] = rls.find('.//ISRC').text    
inner['IRC2'] = rls.find('.//ICPN').text

inner['PPD'] = rls.find('.//AmountPayableInCurrencyOfAccounting').text
inner['Units'] = rls.find('.//GrossNumberOfConsumerSales').text  
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • I would like to thank you for helping me and not closing down this question from the first look. You have been the most helpful person on StackOverflow! :) Can I cast a vote for the most helpful user?Hihi, thank you so much! – I Russu Nov 28 '18 at 08:39
  • I have an issue with the TerritoryCode - it prints the same one, and I needed each one in the file but, it works perfectly otherwise. Many thanks :) – I Russu Nov 28 '18 at 12:06
  • Haha...thanks! Next time before posting, always look at other highly upvoted posts especially in tag areas like `python` `xml` else a less impatient user can downvote and close you out. – Parfait Nov 28 '18 at 19:52
  • Your Github gist XML abbreviates middle sections. Once again, which node repeats? I only see one non-missing *TerritoryCode*. I assumed it was an ancestor item that is static (non-repeating) in entire XML document. – Parfait Nov 28 '18 at 19:54
  • I have edited the GitHub gist - with the whole data for 1st territory, it goes like that till the end. I do hope it makes sense more now. Yes, I have looked everywhere on StackOverflow, for the same issue, I have found few, but it did not worked, that was one of the reason I have posted the question. Thank you :) – I Russu Nov 29 '18 at 09:14
  • See updated text and answer. And what I meant about reviewing SO questions is to see how the better answers are formatted (not their content) as we always ask posters for a [MCVE] (i.e., full XML sample and runnable code with results). – Parfait Nov 29 '18 at 15:50
  • Thank you so much for this and for explaining what you did :). And again, thumbs up for being really really helpful and patient! :) – I Russu Nov 30 '18 at 10:58