convert kiwibank xml into csv for spreadsheet

If xml xslt isn't possible, here's a little snippet of code that takes one argument, an xml file as provided by New Zealand's Kiwibank, and converts it into a semi-colon delimited output. Useful for spreadsheets such as libreoffice calc

Usage: kiwibank-parser.py [xml file path] > output-file.csv

  
#!/usr/bin/env python ################################################################## # Script to convert kiwibank xml file to csv for openoffice calc # ##################################################################

from xml.dom import minidom
import sys

xmldoc = minidom.parse(sys.argv[1])

print 'Date;Amount;Balance;Title;Notes'

for item in xmldoc.getElementsByTagName('Transaction'):
    date = item.childNodes[0].childNodes[0].nodeValue
    title = ''
    amount = ''
    balence = ''
    description = ''
    for line in item.getElementsByTagName('Line'):
        if (line.getAttribute('type') == 'Amount'):
            title = line.childNodes[0].childNodes[0].nodeValue
            amount = line.childNodes[1].childNodes[0].nodeValue
            balence = line.childNodes[2].childNodes[0].nodeValue
        elif (line.getAttribute('type') == 'Description'):
            description = line.childNodes[0].childNodes[0].nodeValue
    print "%s;%s;%s;%s;%s" % (date, amount, balence, title, description)

blogroll

social