#! /usr/bin/python import sys import xml.sax import xml.sax.handler import psycopg2 query = """INSERT INTO sputnik.sputnik (id, direction, position, plane, observer, priority, time, mindistance, maxdistance, observedobject, tags) VALUES (%s, %s, %s, %s, %s, %s, to_timestamp(%s), %s, %s, %s, %s)""" class SputnikHandler(xml.sax.handler.ContentHandler): def __init__(self, c): self._connection = c self._count = 0 self._cursor = self._connection.cursor() def startElement(self, name, attributes): if 'observation' == name: if attributes.has_key('tags'): tt = eval(attributes['tags']) t = '{'+str(tt[0]) for i in tt[1:]: t += ', '+str(i) t += '}' else: t = None d = eval(attributes['direction']) d = '{'+str(d[0])+', '+str(d[1])+', '+str(d[2])+'}' p = eval(attributes['position']) pl = '('+str(p[0])+', '+str(p[2])+')' p = '{'+str(p[0])+', '+str(p[1])+', '+str(p[2])+'}' c = self._cursor c.execute(query, (attributes['id'], d, p, pl, attributes['observer'], attributes['priority'], attributes['time'], attributes['min-distance'], attributes['max-distance'], attributes['observed-object'], t)); def characters(self, data): pass def endElement(self, name): if 'observation' == name: self._count += 1 if self._count % 1000 == 0: self._connection.commit() self._cursor.close() self._cursor = None self._cursor = self._connection.cursor() self._count = 0 def close(self): self._connection.commit() self._cursor.close() self._cursor = None connection = psycopg2.connect("dbname=tomus port=5432") h = SputnikHandler(connection) xml.sax.parse(sys.argv[1], h) h.close() connection.close() connection = None