This repository has been archived by the owner on Jul 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
xml_file.py
112 lines (95 loc) · 3.87 KB
/
xml_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# xml_file.py
#
# Copyright 2019 Jo Hausmann <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from lxml import etree
import time
import os
import sys
########################################################################
# simple create function #
# unique key for filename --> combination of name and insurance number #
# key should be unique --> needs testing #
########################################################################
def create(name, kknummer):
gkey = generate_key(name,kknummer)
root = etree.Element(gkey)
my_tree = etree.ElementTree(root)
write(my_tree,gkey)
########################################################################
# add function --> arguments data = dir(), key = name + insurance #
# open file #
# create child nodes based on keys in parsed dictionary #
########################################################################
def add(data,name,kknummer):
gkey = generate_key(name,kknummer)
try:
tree= etree.parse(gkey + ".xml")#Parse an existing XML Sheet
root = tree.getroot()
except etree.XMLSyntaxError:
print ("File doesn't have root node")
except IOError:
print("File doesn't exist in the directory")
try:
if isinstance(data, dict):
for x,y in data.items():
child = etree.SubElement(root, str(x),value=str(y))
my_tree = etree.ElementTree(root)
write(my_tree,gkey)
except AttributeError:
print("Data structure ist not a dict")
########################################################################
# get function --> returns a dictionary #
# dict is based on xml file #
########################################################################
def get(name,kknummer):
gkey = generate_key(name,kknummer)
try:
data = dict()
tree= etree.parse(gkey + ".xml")
root= tree.getroot()
for child in root:
x=str(child.tag)
y=str(child.get("value"))
data[x]=y
return (data)
except etree.XMLSyntaxError:
print ("File doesn't have root node")
except IOError:
print("File doesn't exist in the directory")
#########################################################
# write function #
# check whether given tree is instance of etree class #
# write tree as xml file --> pretty_print #
#########################################################
def write(my_tree,gkey):
if isinstance(my_tree,etree._ElementTree):
f = open(gkey + ".xml", "wb")
f.write(etree.tostring(my_tree, pretty_print=True))
f.close()
else:
os.error("Given argument is not object of class etree")
#########################################################
# key method #
#########################################################
def generate_key(name, kknummer):
return (name + "_" + kknummer)