convert.py 2.16 KB
import untangle
import sys
import os
import argparse
import sqlite3
import shlex

#  To Install:
#     * Install python 2.7.X
#     * pip install untangle

# Edit the fields below for each file. If there are other files just add them to the hash.

field_list = {
	"items-armor.xml":
		["id", "flags", "name", "description", "level", "iLevel", "slots", "races"],
	"items-armor2.xml":
		["id", "flags", "name", "description", "level", "iLevel", "slots", "races"],
	"items-currency.xml":
		["id", "flags", "name", "description", "log-name-singular", "log-name-plural"],
	"items-general.xml":
		["id", "flags", "name", "description"],
	"items-general2.xml":
		["id", "flags", "name", "description"],
	"items-puppet.xml":
		["id", "flags", "name", "description", "puppet-slot", "element-charge"],
	"items-usable.xml":
		["id", "flags", "name", "description", "activation-time"],
	"items-voucher-slip.xml":
		["id", "flags", "name", "description", "valid-targets"],
	"items-weapons.xml":
		["id", "flags", "name", "description", "level", "iLevel", "slots", "races", "damage"],

	}

parser = argparse.ArgumentParser()
parser.add_argument('-s', '--sourcexml', required=True, help="Source XML Directory")
parser.add_argument('-o', '--outputdb', required=True, help="Destination SQLite Database")
args = parser.parse_args()

print "Source XML Directory: %s" % (args.sourcexml,)
print "Destination SQLite DB: %s" % (args.outputdb,)

conn = sqlite3.connect(args.outputdb)
c = conn.cursor()

for filename in os.listdir(args.sourcexml):
	if filename.endswith(".xml"):
		try:
			fields = (', '.join('"' + item + '"' for item in field_list[filename]))
			query = "CREATE TABLE \"%s\" (%s)" % (filename, fields)
			print query
			c.execute(query)
			conn.commit()
		except sqlite3.OperationalError:
			pass
		source_file = os.path.join(args.sourcexml, filename)
		print "Parsing File: %s" % (source_file,)
		doc = untangle.parse(source_file)
		for item in doc.thing_list.thing:
			row = []
			for field in item.children:
				if field['name'] in field_list[filename]:
					row.append(field.cdata)
			
			query = "INSERT INTO \"%s\" VALUES(%s)" % (filename, ','.join('?' * len(row)))
			c.execute(query, row)
		conn.commit()

c.close()