Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

xml to sqlite

from optparse import OptionParser
from xml.dom.minidom import parse
import os
import sqlite3


datatypeMap = {
  'integer': 'INT',
  'datetime': 'DATETIME',
  'boolean': 'BOOLEAN'
}

defaultDataType = 'TEXT'


def get_xml_doms(directory):
  result = []
  for filename in directory:
    if filename.endswith('.xml'):
      dom = parse(filename)
      result.append(dom)
  return result  

def yield_db_schema(dbDef):
  result = ''
  for (table, tableDef) in dbDef.items():
    result += create_table(table, tableDef) 
    
  return result

def exec_create_schema(dbDef, conn, db):
  for (table, tableDef) in dbDef.items():
    create = create_table(table, tableDef) 
    db.execute(create)
    
def yield_inserts(recordSet):
  inserts = ''
  for (table, rows) in recordSet.items():
    for row in rows:
      fields = "'" + '', ''.join(row.keys()) + "'"
      data = "'" + '', ''.join(row.values()) + "'"
      if fields != "''":
        inserts += "INSERT INTO '%s' (%s) VALUES (%s);
" % (table, fields, data)
  
  return inserts
  
def exec_insert(recordSet, conn, db):
  for (table, rows) in recordSet.items():
    for row in rows:
        
      fields = "'" + '', ''.join(row.keys()) + "'"
      data = "'" + '', ''.join(row.values()) + "'"
      if len(row.keys()) >0:
        marklist = ["?"] * len(row.keys())
        marks = ', '.join(marklist)
          
        insert = "INSERT INTO '%s' (%s) VALUES (%s)" %  (table, fields, marks)
        values = tuple(row.values())
        db.execute(insert, values)
        conn.commit()
        

def create_table(table, tableDef):
  fields = []
  begin = 'CREATE TABLE '%s' ( 
' % table
  for field, fieldDef in tableDef.items():
    fields.append(create_field(field, fieldDef))
  end = '
);

'  
  result = begin + ',
'.join(fields) + end
  
  return result

def create_field(field, fieldDef):
  if (u'type') in fieldDef:
    datatype =  fieldDef.get(u'type')
  else:
    datatype = defaultDataType
  
  return "  '%s' %s" % (field, datatype) 

def collect_structure(doms):
  db = {}
  records = {}
  for dom in doms:
    db = gen_db_struct(dom.childNodes, db)
 
  return db
  
def collect_data(dbDef, doms):
  recordset = {}
  for dom in doms:
    for (table, fieldDef) in dbDef.items():
      if not table in recordset:
        recordset[table] = []
      for row in dom.getElementsByTagName(table):
        record = {}
        for (column, _) in fieldDef.items():
          for node in row.getElementsByTagName(column):
            if node.hasChildNodes():
              for item in node.childNodes:
                if hasattr(item, 'data'):
                  if len(item.data.strip()) > 0:
                    record[column] = item.data
              
        recordset[table].append(record)
  
  return recordset
              

def gen_db_struct(nodeList, db = {}):
  for node in nodeList:
    if not node.hasChildNodes() and node.parentNode.parentNode.nodeName != '#document':
      # a new field of data
      field = node.parentNode
      fieldName = field.nodeName
      table = field.parentNode
      tableName = table.nodeName
        
      if not tableName in db:
        db[tableName] = {}
  
      db[tableName][fieldName] = {}

      if field.hasAttributes():
        for (Key, Value) in field.attributes.items():
          if Key != u'type' and Value != u'array':
            db[tableName][fieldName][Key] = datatypeMap[Value]   
    else:
      gen_db_struct(node.childNodes, db)
      
  return db

def run(inputDir, outputFile):
  files = []
  for filename in os.listdir(inputDir):
    files.append(os.path.join(inputDir, filename))
    
  domList = get_xml_doms(files)
  dbDef = collect_structure(domList)
  records = collect_data(dbDef, domList)
  conn = sqlite3.connect(outputFile)
  db = conn.cursor()
  exec_create_schema(dbDef, conn, db)
  exec_insert(records, conn, db)
  db.close()
  
def main():
  usage = "usage: %prog [options] /path/to/dir/with/xml"
  parser = OptionParser(usage)
  parser.add_option("-f", "--file", dest="outputFile", default = 'xmlsqlite.db3',
                    help="Specify the filename for the sqlite database.  It will be created if it does not exist [Default: xmlsqlite.db3]")

  (options, args) = parser.parse_args()
  if len(args) != 1:
      parser.error("incorrect number of arguments")
  
  inputDir = os.path.abspath(os.path.expanduser(args[0]))
  run(inputDir, options.outputFile)

  
if __name__ == "__main__": main()
Comment

PREVIOUS NEXT
Code Example
Python :: Implement a function word_list() that reads the 5_letter_words.txt file and returns a list of the words in the file. 
Python :: Reading from a file way01 
Python :: Improve the Request Handle Errors 
Python :: dont print pip jupyter 
Python :: python if statement syntax 
Python :: Python Global variable and Local variable with same name 
Python :: como utilizar activar grepper en visual studio code python 
Python :: pip set mirror site 
Python :: Python Decorating Functions with Parameters 
Python :: Python ValueError in strptime() 
Python :: tornado cookies authorization 
Python :: Delete file to trash 
Python :: nvidia-smi with user name 
Python :: sample mapping in pandas 
Python :: pairwiseclip arcpy 
Python :: parsing date columns when reading csv 
Python :: methods accesory python 
Python :: pandas to_csv adds unnamed column 
Python :: if you have a list and the user input one of the keys then output its value 
Python :: HIDING AND ENCRYPTING PASSWORDS IN PYTHON USING MASKPASS MODULE 
Python :: docker python no module named 
Python :: subprocess readline blocking problem 
Python :: vectorized function 
Python :: Donut chart graphing funciton 
Python :: python sliding window maximum 
Python :: captcha.image install in python 
Python :: how to draw squircle python 
Python :: numpy get length of list 
Python :: if condition in djangio template 
Python :: print less than specific number in one row python 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =