forked from binux/libMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XML2Object.py
163 lines (131 loc) · 4.17 KB
/
XML2Object.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from xml.etree import ElementTree
import re
class object_dict(dict):
def __init__(self, initd=None):
if initd is None:
initd = {}
dict.__init__(self, initd)
def __getattr__(self, item):
return self.__getitem__(item)
def __getitem__(self, item):
try:
val = dict.__getitem__(self, item)
return val
except:
None
def __iter__(self):
yield self
return
def __setattr__(self, item, value):
self.__setitem__(item, value)
class XML2Object(object_dict):
def __init__(self, xml):
self.dom = XML2dict().fromstring(xml)
object_dict.__init__(self, self.dom)
def dfs(obj, name):
lst = XMLObject()
if isinstance(obj, dict):
if name in obj:
if isinstance(obj[name], list):
lst += obj[name]
else:
lst.append(obj[name])
for item in obj.values():
lst += dfs(item, name)
elif isinstance(obj, list):
for item in obj:
lst += dfs(item, name)
return lst
class UUObject(list):
def __init__(self, init = []):
if isinstance(init,list):
list.__init__(self, init)
else:
list.__init__(self, [init, ])
def __getattr__(self, name):
try:
return list.__getattr__(self, name)
except:
try:
if name in dir(self[0]):
return getattr(self[0],name)
except:
pass
return self[name]
def __getitem__(self, name):
if isinstance(name,int):
return list.__getitem__(self, name)
else:
return dfs(self,name)
def val(self):
if len(self)>0:
return self[0]
else:
return None
def __unicode__(self):
if len(self)>0:
return unicode(self[0])
return u""
def __str__(self):
return unicode(self).encode("u8")
def __int__(self):
return int(str(self))
def eq(self, index):
if len(self)<index:
return self[index]
else:
return None
def find(self, func):
for item in self:
if func(item):
return item
def findall(self, func):
return UUObject(filter(func, self))
class XMLObject(UUObject):
def __init__(self, xml = None):
if xml:
UUObject.__init__(self,[XML2dict().fromstring(xml),])
else:
UUObject.__init__(self,[])
class XML2dict(object):
def __init__(self):
pass
def _parse_node(self, node):
node_tree = object_dict()
# Save attrs and text, hope there will not be a child with same name
if node.text:
node_tree.value = node.text
for (k, v) in node.attrib.items():
k, v = self._namespace_split(k, object_dict({'value': v}))
node_tree[k] = v
# Save childrens
for child in node.getchildren():
tag, tree = self._namespace_split(
child.tag, self._parse_node(child))
if tag not in node_tree: # the first time, so store it in dict
node_tree[tag] = tree
continue
old = node_tree[tag]
if not isinstance(old, list):
node_tree.pop(tag)
node_tree[tag] = [
old, ] # multi times, so change old dict to a list
node_tree[tag].append(tree) # add the new one
if "value" in node_tree and len(node_tree) == 1:
return node_tree["value"]
return node_tree
def _namespace_split(self, tag, value):
result = re.compile("\{(.*)\}(.*)").search(tag)
if result:
print tag
value.namespace, tag = result.groups()
return (tag, value)
def parse(self, file):
f = open(file, 'r')
return self.fromstring(f.read())
def fromstring(self, s):
t = ElementTree.fromstring(s)
root_tag, root_tree = self._namespace_split(t.tag, self._parse_node(t))
return object_dict({root_tag: root_tree})