[code.view]

[top] / python / PyMOTW / xml / etree / ElementTree / ElementTree_podcast_csv_treebuilder.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Convert XML list of podcasts to a CSV file.
     """
     #end_pymotw_header
     
     import csv
     from xml.etree.ElementTree import XMLTreeBuilder
     import sys
     
     class PodcastListToCSV(object):
     
         def __init__(self, outputFile):
             self.writer = csv.writer(outputFile, quoting=csv.QUOTE_NONNUMERIC)
             self.group_name = ''
             return
     
         def start(self, tag, attrib):
             if tag != 'outline':
                 # Ignore anything not part of the outline
                 return
             if not attrib.get('xmlUrl'):
                 # Remember the current group
                 self.group_name = attrib['text']
             else:
                 # Output a podcast entry
                 self.writer.writerow( (self.group_name, attrib['text'],
                                        attrib['xmlUrl'],
                                        attrib.get('htmlUrl', ''),
                                        )
                                       )
     
         def end(self, tag):
             # Ignore closing tags
             pass
         def data(self, data):
             # Ignore data inside nodes
             pass
         def close(self):
             # Nothing special to do here
             return
     
     
     target = PodcastListToCSV(sys.stdout)
     parser = XMLTreeBuilder(target=target)
     with open('podcasts.opml', 'rt') as f:
         for line in f:
             parser.feed(line)
     parser.close()
     

[top] / python / PyMOTW / xml / etree / ElementTree / ElementTree_podcast_csv_treebuilder.py

contact | logmethods.com