Skip to content

Instantly share code, notes, and snippets.

@ezaurum
Created September 11, 2015 04:14
Show Gist options
  • Save ezaurum/61d748130f185c964960 to your computer and use it in GitHub Desktop.
Save ezaurum/61d748130f185c964960 to your computer and use it in GitHub Desktop.
Xml file insert to DB
import postgresql.driver as pg_driver
import xml.etree.ElementTree as etree
import sys
__author__ = 'ezaurum'
type_map = {'PlayerSpawnPoint': 0, 'Gathering': 1, 'NPC': 2}
xml_file_name = sys.argv[1]
tree = etree.parse(open(xml_file_name, 'r'))
root = tree.getroot()
db = pg_driver.connect(
user='user id',
password='user password',
host='127.0.0.1',
port='5432')
ps = db.prepare(
"INSERT INTO gamespot (id, zoneid, spottype, x, y, z, anglex, angley, anglez, anglew, targetclassid, actionradius, spawnmax, spawninterval, spawnradius, movableradius, autorespwan) "
"VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)")
psd = db.prepare("DELETE FROM gamespot WHERE id=$1")
zone_id = int(xml_file_name[2:-4])
for element in root.find("entities").findall("entity"):
spot_id = int(element.findtext("DisposeID"))
type_number = type_map.get(element.findtext("DisposeType"), 0)
position_string = element.findtext("Position").split(',')
target_class_id = int(element.findtext("CID"))
px = float(position_string[0])
pz = float(position_string[1])
py = float(position_string[2])
rotation_string = element.findtext("Rotation").split(',')
rx = float(rotation_string[0])
rz = float(rotation_string[1])
ry = float(rotation_string[2])
rw = float(rotation_string[3])
findtext = element.findtext("SpawnMax")
if findtext is None:
spawn_max = 0
else:
spawn_max = int(findtext)
spawn_interval_text = element.findtext("SpawnInterval")
if spawn_interval_text is None:
spawn_interval = 0
else:
spawn_interval = float(spawn_interval_text)
spawn_radius_text = element.findtext("SpawnRadius")
if spawn_radius_text is None:
spawn_radius = 0
else:
spawn_radius = float(spawn_radius_text)
action_radius_text = element.findtext("ActionRadius")
if action_radius_text is None:
action_radius = 0
else:
action_radius = float(action_radius_text)
movable_radius_text = element.findtext("MovableRadius")
if movable_radius_text is None:
movable_radius = 0
else:
movable_radius = float(movable_radius_text)
psd(spot_id)
ps(spot_id, zone_id, type_number, px, py, pz, rx, ry, rz, rw, target_class_id, action_radius, spawn_max,
spawn_interval, spawn_radius, movable_radius, True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment