29 atlas_objects=parseXML("input XML string here")
31 resulting atlas_objects has 0 or more atlas objects
34 NOTE: you need to make separate parser for each stream (for example
35 when several clients connect)
53 def setup(self, stream_flag=None):
54 """uses tree that start from root_obj, current route to leave
55 is kept in obj_stack"""
63 decoder.BaseDecoder.setup(self, stream_flag)
65 def set_stream_mode(self, mode=1):
66 decoder.BaseDecoder.set_stream_mode(self, mode)
93 return not [_f
for _f
in [ch
not in string.whitespace
for ch
in self.
data]
if _f]
and self.
obj_stack == [[]]
95 def unknown_starttag(self, tag, attributes):
99 """#PCDATA (actual string,int,float,uri content)"""
102 def startElement(self, tag, attributes):
109 elif tag ==
"string":
116 def endElement(self, tag):
123 elif tag ==
"string":
131 def start_atlas(self):
138 """for int/float/string: save name if have one"""
139 self.
name_stack.append(attributes.get(
"name",
""))
143 """put value into mapping/list"""
150 raise XmlException(
"attribute outside mapping (%s:%s)!" % \
152 setattr(obj, name, value)
154 if not isinstance(obj, list):
159 """for list/map: add to stack"""
164 """for list/map: remove from stack"""
176 def start_int(self, attributes):
181 value = int(self.
data)
183 value = int(self.
data)
186 def start_float(self, attributes):
192 def start_string(self, attributes):
195 def end_string(self):
198 def start_list(self, attributes):
204 def start_map(self, attributes):
211 def string2object(string):
212 """convert a string of one entity into an object"""
217 return parse(string)[0]
220 def parse_string_to_dict(string, full_stream=0):
221 """convert a string of entities into a dict of entites"""
223 parse = get_decoder(full_stream)
230 all_objects = parse(string)
236 for obj
in all_objects:
237 objects[obj.id] = obj
243 def read_file_to_dict(file):
245 objects = parse_string_to_dict(fp.read(), full_stream=1)
250 def get_decoder(stream_flag=None):
252 xml_msg_parser.setup(stream_flag)
253 return xml_msg_parser
255 def parse_stream(data):
257 xml.sax.parseString(data, handler)