def parse(self, file, baseURI): from xml.parsers.expat import ParserCreate parser = ParserCreate(namespace_separator="") parser.returns_unicode = 0 dh = DocumentHandler(parser, self.add) dh.set_base(baseURI) parser.ParseFile(file) file.close()
def parse(self, text): from xml.parsers.expat import ParserCreate parser = ParserCreate() # no attribute returns_unicode in Python3 if hasattr(parser, "returns_unicode"): parser.returns_unicode = 0 parser.StartElementHandler = self.startElementHandler parser.EndElementHandler = self.endElementHandler parser.Parse(text)
def parse_gifti_file(fname): datasource = open(fname,'r') global img global parser global out img = None parser = ParserCreate() parser.buffer_text = True parser.buffer_size = 1000000 HANDLER_NAMES = [ 'StartElementHandler', 'EndElementHandler', 'CharacterDataHandler', ] out = Outputter() for name in HANDLER_NAMES: setattr(parser, name, getattr(out, name)) try: parser.ParseFile(datasource) except ExpatError: print 'An error occured while parsing Gifti file.' # update filename img.filename = fname return img
def _fetchUnicodes(glyphPath): # Given a path to an existing .glif file, get a list of all # unicode values from the XML data. # NOTE: this assumes .glif files written by glifLib, since # we simply stop parsing as soon as we see anything else than # <glyph>, <advance> or <unicode>. glifLib always writes those # elements in that order, before anything else. from xml.parsers.expat import ParserCreate unicodes = [] def _startElementHandler(tagName, attrs, _unicodes=unicodes): if tagName == "unicode": _unicodes.append(int(attrs["hex"], 16)) elif tagName not in ("glyph", "advance"): raise _DoneParsing() p = ParserCreate() p.StartElementHandler = _startElementHandler p.returns_unicode = True f = open(glyphPath) try: p.ParseFile(f) except _DoneParsing: pass return unicodes
def parse(self, data): parser = ParserCreate("UTF-8", " ") parser.buffer_text = True parser.StartElementHandler = self.StartElementHandler parser.EndElementHandler = self.EndElementHandler parser.Parse(data) return self.map
def __init__(self): self.recent_files = [] self.tlist = [] self.item = None fname = os.path.expanduser(GRAMPS_FILENAME) if not os.path.exists(fname): return # it's the first time gramps has ever been run try: with open(fname, "rb") as xml_file: if USE_LOCK: fcntl.lockf(xml_file, fcntl.LOCK_SH) parser = ParserCreate() parser.StartElementHandler = self.start_element parser.EndElementHandler = self.end_element parser.CharacterDataHandler = self.characters parser.ParseFile(xml_file) # all advisory locks on a file are released on close except IOError as err: logging.warning( _("Unable to open list of recent DBs file {fname}: {error}" ).format(fname=fname, error=err)) except ExpatError as err: logging.error( _("Error parsing list of recent DBs from file {fname}: " "{error}.\nThis might indicate a damage to your files.\n" "If you're sure there is no problem with other files, " "delete it, and restart Gramps." ).format(fname=fname, error=err))
def parse(self, text): from xml.parsers.expat import ParserCreate parser = ParserCreate() parser.returns_unicode = 0 parser.StartElementHandler = self.startElementHandler parser.EndElementHandler = self.endElementHandler parser.Parse(text)
def __init__(self): self.recent_files = [] fname = os.path.expanduser(WEARNOW_FILENAME) if not os.path.exists(fname): return # it's the first time wearnow has ever been run try: with open(fname, "rb") as xml_file: if use_lock: fcntl.lockf(xml_file,fcntl.LOCK_SH) p = ParserCreate() p.StartElementHandler = self.startElement p.EndElementHandler = self.endElement p.CharacterDataHandler = self.characters p.ParseFile(xml_file) # all advisory locks on a file are released on close except IOError as err: logging.warning( _("Unable to open list of recent DBs file {fname}: {error}" ).format(fname=fname, error=err)) except Exception as err: logging.error( _("Error parsing list of recent collections from file {fname}: {error}.\n" "This might indicate a damage to your files.\n" "If you're sure there is no problem with other files, " "delete it, and restart ComfiSense." ).format(fname=fname, error=err))
def listing_query(self, query): data = '<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.nwmls.com/EverNetServices" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><ns0:Body><ns1:{0}><ns1:v_strXmlQuery>{1}</ns1:v_strXmlQuery></ns1:{0}></ns0:Body></SOAP-ENV:Envelope>'.format(self.endpoint, escape(str(query))) response = session.post('http://evernet.nwmls.com/evernetqueryservice/evernetquery.asmx', headers = { 'Soapaction': "http://www.nwmls.com/EverNetServices/{0}".format(self.endpoint), 'Content-Type': 'text/xml; charset=utf-8', }, timeout=60*5, data=data, stream=True) logger.info('Sent request...') logger.debug('>>> %s' % data) with tempfile.TemporaryFile() as response_file: tmp = response.raw.read(1000) logger.info('Receiving data...') while tmp: response_file.write(tmp) tmp = response.raw.read(1000) logger.debug('<<< %s' % tmp) response_file.seek(0) self.data = tempfile.TemporaryFile() self.in_envelope = False p = ParserCreate() p.StartElementHandler = self._start_element_get_envelope p.EndElementHandler = self._end_element_get_envelope p.CharacterDataHandler = self._char_data_get_envelope response_file.seek(0) try: p.ParseFile(response_file) except ExpatError: logging.error("Error parsing XML response from Evernet. This usually happens when the server returns an error message. You can see the full response if you set the logging to DEBUG level") raise logger.info('All data received, starting parsing') self.data.seek(0) return self.data
def photo_query(self, query): data = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><RetrieveImages xmlns="NWMLS:EverNet"><query>{0}</query></RetrieveImages></soap:Body></soap:Envelope>'.format( escape(query) ) import socket socket.setdefaulttimeout(30) conn = httplib.HTTPConnection("images.idx.nwmls.com", timeout=30) conn.putrequest("POST", "/imageservice/imagequery.asmx") conn.putheader("Content-Type", "text/xml; charset=utf-8") conn.putheader("Soapaction", "NWMLS:EverNet/RetrieveImages") conn.putheader("Content-Length", "%d" % len(data)) conn.endheaders() conn.send(data) logger.debug(">>> %s" % data) with tempfile.TemporaryFile() as response_file: response = conn.getresponse() tmp = response.read(1000) while tmp: response_file.write(tmp) tmp = response.read(1000) logger.debug("<<< %s" % tmp) response_file.seek(0) self.data = tempfile.TemporaryFile() self.in_envelope = False p = ParserCreate() p.StartElementHandler = self._start_element_get_envelope p.EndElementHandler = self._end_element_get_envelope p.CharacterDataHandler = self._char_data_get_envelope p.ParseFile(response_file) self.data.seek(0) return self.data
def photo_query(self, query): data = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><RetrieveImages xmlns="NWMLS:EverNet"><query>{0}</query></RetrieveImages></soap:Body></soap:Envelope>'.format(escape(query)) response = session.post('http://images.idx.nwmls.com/imageservice/imagequery.asmx', headers = { 'Content-Type': 'text/xml; charset=utf-8', 'Soapaction':'NWMLS:EverNet/RetrieveImages', }, data=data, timeout=30, stream=True) logger.debug('>>> %s' % data) with tempfile.TemporaryFile() as response_file: tmp = response.raw.read(1000) while tmp: response_file.write(tmp) tmp = response.raw.read(1000) logger.debug('<<< %s' % tmp) response_file.seek(0) self.data = tempfile.TemporaryFile() self.in_envelope = False p = ParserCreate() p.StartElementHandler = self._start_element_get_envelope p.EndElementHandler = self._end_element_get_envelope p.CharacterDataHandler = self._char_data_get_envelope p.ParseFile(response_file) self.data.seek(0) return self.data
def parseDTDString(data, path): result = [] currentComment = [None] parser = ParserCreate() parser.UseForeignDTD(True) parser.SetParamEntityParsing(XML_PARAM_ENTITY_PARSING_ALWAYS) def ExternalEntityRefHandler(context, base, systemId, publicId): subparser = parser.ExternalEntityParserCreate(context, 'utf-8') subparser.Parse(data.encode('utf-8'), True) return 1 def CommentHandler(data): currentComment[0] = data.strip() def EntityDeclHandler(entityName, is_parameter_entity, value, base, systemId, publicId, notationName): result.append((unescapeEntity(entityName), currentComment[0], unescapeEntity(value.strip()))) currentComment[0] = None parser.ExternalEntityRefHandler = ExternalEntityRefHandler parser.CommentHandler = CommentHandler parser.EntityDeclHandler = EntityDeclHandler parser.Parse('<!DOCTYPE root SYSTEM "foo"><root/>', True) for entry in result: yield entry
def __init__(self, filename): self.obj = mesh.printableObject(os.path.basename(filename)) self.mesh = self.obj._addMesh() r = ParserCreate() r.StartElementHandler = self._StartElementHandler r.EndElementHandler = self._EndElementHandler r.CharacterDataHandler = self._CharacterDataHandler self._base = {} self._cur = self._base self._idMap = {} self._geometryList = [] self._faceCount = 0 r.ParseFile(open(filename, "r")) self.vertexCount = 0 for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']: for node in self._idMap[instance_visual_scene['_url']]['node']: self._ProcessNode1(node) self.mesh._prepareFaceCount(self._faceCount) for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']: for node in self._idMap[instance_visual_scene['_url']]['node']: self._ProcessNode2(node) scale = float(self._base['collada'][0]['asset'][0]['unit'][0]['_meter']) * 1000 self.mesh.vertexes *= scale self._base = None self._cur = None self._idMap = None self.obj._postProcessAfterLoad()
def processReport(xmlFile): global reportData, tagStack guid = os.path.splitext(os.path.basename(xmlFile))[0] cursor = get_db().cursor() executeQuery(cursor, '''SELECT guid FROM #PFX#reports WHERE guid = %s''', (guid)) report = cursor.fetchone() if report != None: os.remove(xmlFile) return source = open(xmlFile, 'rb') reportData = {'status': '', 'usefulness': 0, 'warnings': {}, 'requests': [], 'filters': [], 'subscriptions': [], 'extensions': [], 'errors': [], 'time': time()} tagStack = [] parser = ParserCreate() parser.StartElementHandler = processElementStart parser.EndElementHandler = processElementEnd parser.CharacterDataHandler = processText try: parser.ParseFile(source) except ExpatError, error: reportData['warnings']['!parsing'] = 'Parsing error in the report: %s at line %i column %i' % (ErrorString(error.code), error.lineno, error.offset)
def __init__(self, filename): self.mesh = Mesh() self.mesh.metaData['filename'] = filename self.volume = self.mesh.newVolume() r = ParserCreate() r.StartElementHandler = self._StartElementHandler r.EndElementHandler = self._EndElementHandler r.CharacterDataHandler = self._CharacterDataHandler self._base = {} self._cur = self._base self._idMap = {} self._geometryList = [] self._faceCount = 0 r.ParseFile(open(filename, "r")) self.vertexCount = 0 for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']: for node in self._idMap[instance_visual_scene['_url']]['node']: self._ProcessNode1(node) self.volume._prepareFaceCount(self._faceCount) for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']: for node in self._idMap[instance_visual_scene['_url']]['node']: self._ProcessNode2(node) scale = float(self._base['collada'][0]['asset'][0]['unit'][0]['_meter']) * 1000 self.volume.vertexData *= scale self._base = None self._cur = None self._idMap = None self.volume.calculateNormals()
def parse_gifti_file(fname, buffer_size = 35000000): """ Parse gifti file named `fname`, return image Parameters ---------- fname : str filename of gifti file buffer_size: int, optional size of read buffer. Returns ------- img : gifti image """ datasource = open(fname,'rb') parser = ParserCreate() parser.buffer_text = True parser.buffer_size = buffer_size HANDLER_NAMES = ['StartElementHandler', 'EndElementHandler', 'CharacterDataHandler'] out = Outputter() for name in HANDLER_NAMES: setattr(parser, name, getattr(out, name)) try: parser.ParseFile(datasource) except ExpatError: print 'An expat error occured while parsing the Gifti file.' # update filename out.img.filename = fname return out.img
def load(self, filename): r = ParserCreate() r.StartElementHandler = self._StartElementHandler r.EndElementHandler = self._EndElementHandler r.CharacterDataHandler = self._CharacterDataHandler self._base = {} self._cur = self._base self._idMap = {} self._geometryList = [] r.ParseFile(open(filename, "r")) self.vertexCount = 0 for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']: for node in self._idMap[instance_visual_scene['_url']]['node']: self._ProcessNode1(node) self._prepareVertexCount(self.vertexCount) for instance_visual_scene in self._base['collada'][0]['scene'][0]['instance_visual_scene']: for node in self._idMap[instance_visual_scene['_url']]['node']: self._ProcessNode2(node) scale = float(self._base['collada'][0]['asset'][0]['unit'][0]['_meter']) * 1000 self.origonalVertexes *= scale self._base = None self._cur = None self._idMap = None self._postProcessAfterLoad() return self
def get_parser(self): p = ParserCreate() p.buffer_text = True p.StartElementHandler = self.start_element p.EndElementHandler = self.end_element p.CharacterDataHandler = self.char_data return p
def weather_parser(xml): handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml) print(handler.weather)
def parse_weather(xml): handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml) return weather_dict
def parse(self, fileobj): from xml.parsers.expat import ParserCreate parser = ParserCreate() parser.StartElementHandler = self.handleBeginElement parser.EndElementHandler = self.handleEndElement parser.CharacterDataHandler = self.handleData parser.ParseFile(fileobj) return self.root
def parse ( self, xmldata ): from xml.parsers.expat import ParserCreate p = ParserCreate ( 'utf-8' ) p.StartElementHandler = self.start_element p.EndElementHandler = self.end_element p.CharacterDataHandler = self.char_data p.Parse ( xmldata ) return '\n'.join ( self.output )
def xml_test(): #操作XML有两种方法:DOM和SAX。 #DOM会把整个XML读入内存,解析为树,因此占用内存大,解析慢,优点是可以任意遍历树的节点。 #SAX是流模式,边读边解析,占用内存小,解析快,缺点是我们需要自己处理事件。 #在Python中使用SAX解析XML非常简洁,通常我们关心的事件是start_element,end_element和char_data class DefaultSaxHandler(object): def __init__(self): self.weather = {} def start_element(self,name,attrs): if name=='yweather:location': self.weather['country'] = attrs['country'] self.weather['city'] = attrs['city'] if name=='yweather:astronomy': self.weather['sunrise'] = attrs['sunrise'] self.weather['sunset'] = attrs['sunset'] print(self.weather) #print('SAX: start_element:%s, attrs:%s' % (name,str(attrs))) def end_element(self,name): pass#print('SAX:end_element:%s' % name) def char_data(self,text): if not text: print('SAX:char_data:%s' % text) data = r'''<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"> <channel> <title>Yahoo! Weather - Beijing, CN</title> <lastBuildDate>Wed, 27 May 2015 11:00 am CST</lastBuildDate> <yweather:location city="Beijing" region="" country="China"/> <yweather:units temperature="C" distance="km" pressure="mb" speed="km/h"/> <yweather:wind chill="28" direction="180" speed="14.48" /> <yweather:atmosphere humidity="53" visibility="2.61" pressure="1006.1" rising="0" /> <yweather:astronomy sunrise="4:51 am" sunset="7:32 pm"/> <item> <geo:lat>39.91</geo:lat> <geo:long>116.39</geo:long> <pubDate>Wed, 27 May 2015 11:00 am CST</pubDate> <yweather:condition text="Haze" code="21" temp="28" date="Wed, 27 May 2015 11:00 am CST" /> <yweather:forecast day="Wed" date="27 May 2015" low="20" high="33" text="Partly Cloudy" code="30" /> <yweather:forecast day="Thu" date="28 May 2015" low="21" high="34" text="Sunny" code="32" /> <yweather:forecast day="Fri" date="29 May 2015" low="18" high="25" text="AM Showers" code="39" /> <yweather:forecast day="Sat" date="30 May 2015" low="18" high="32" text="Sunny" code="32" /> <yweather:forecast day="Sun" date="31 May 2015" low="20" high="37" text="Sunny" code="32" /> </item> </channel> </rss> ''' handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(data)
def __init__(self, text): from xml.parsers.expat import ParserCreate self.unicodes = [] self._elementStack = [] parser = ParserCreate() parser.returns_unicode = 0 # XXX, Don't remember why. It sucks, though. parser.StartElementHandler = self.startElementHandler parser.EndElementHandler = self.endElementHandler parser.Parse(text)
def raising_external_ref_handler(*args, **kwargs): parser = ParserCreate(*args, **kwargs) parser.ExternalEntityRefHandler = lambda *x: 0 try: feature = "http://apache.org/xml/features/disallow-doctype-decl" parser._reader.setFeature(feature, True) except AttributeError: pass return parser
def parse_weather(data): parser = ParserCreate() handler = WeatherSaxHandler() parser.StartElementHandler = handler.start_e parser.EndElementHandler = handler.end_e parser.CharacterDataHandler = handler.c_data handler.init() parser.Parse(data) return handler.weather
def _create_parser(self): """Internal function that allows subclasses to mess with the underlying parser, if desired.""" parser = ParserCreate(encoding=self.encoding) # from xml package parser.buffer_text = True if self.buffer_size is not None: parser.buffer_size = self.buffer_size return parser
def getPermissiondict(self): for appname,xmlpath in self.dict.items(): self.permission = [] parser = ParserCreate() parser.StartElementHandler = self.start_element f1 = open(xmlpath,"rb") parser.ParseFile(f1) self.dict[appname] = self.permission f1.close()
def _parse(file, name, handler_class): parser = ParserCreate(namespace_separator="^") parser.returns_unicode = 0 documentHandler = handler_class(parser, name) parser.ParseFile(file) file.close() return documentHandler.module
def process_one_file(filename): global my_article_terms print "Processing", filename p=ParserCreate() p.StartElementHandler=start_element p.EndElementHandler=end_element p.CharacterDataHandler=character_data my_article_terms={} p.ParseFile(gzip.GzipFile(filename)) return my_article_terms
class XmlTagUrlParser(object): """Parse XML files and find URLs in text content of a tag name.""" def __init__(self, tag): """Initialize the parser.""" self.tag = tag self.parser = ParserCreate() self.parser.buffer_text = True try: self.parser.returns_unicode = True except AttributeError: pass # Python 3 self.parser.StartElementHandler = self.start_element self.parser.EndElementHandler = self.end_element self.parser.CharacterDataHandler = self.char_data def parse(self, url_data): """Parse XML URL data.""" self.url_data = url_data self.in_tag = False self.url = u"" data = url_data.get_raw_content() isfinal = True try: self.parser.Parse(data, isfinal) except ExpatError as expaterr: self.url_data.add_warning(expaterr.message, tag=WARN_XML_PARSE_ERROR) def start_element(self, name, attrs): """Set tag status for start element.""" self.in_tag = (name == self.tag) self.url = u"" def end_element(self, name): """If end tag is our tag, call add_url().""" self.in_tag = False if name == self.tag: self.add_url() def add_url(self): """Add non-empty URLs to the queue.""" if self.url: self.url_data.add_url(self.url, line=self.parser.CurrentLineNumber, column=self.parser.CurrentColumnNumber) self.url = u"" def char_data(self, data): """If inside the wanted tag, append data to URL.""" if self.in_tag: self.url += data
def parseXml(xml_str): handler = YWeather() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_element parser.Parse(xml_str) return handler.result
def handle_success(self, response): handler = CommonSaxHandler(self._root_ele_names, self._ele_names) parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_element parser.Parse(response.text) return CommonXmlParseResult(handler.root_ele_name, handler.elements)
def parse_weather(data): handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.satrt_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(data) return handler.get_data
def videoXml(xml_str): handler = GetVideoByXml() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.CharacterDataHandler = handler.char_element parser.EndElementHandler = handler.end_element parser.Parse(xml_str) return handler.video
def parseXml(xml_str): handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml_str) return {'city': handler.location['city'], 'forecast': handler.args}
def parse_weather(xml): handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml) return handler.weather
def parseXml(xml_str): handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml_str) return res
def handler_xmldata(xmldata): handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xmldata) return {'city':handler.city, "forecast":handler.forecast}
def parse(cls, fname: Text) -> 'FMXml': """parse Nodes from xml. """ ret = FMXml() parser = ParserCreate() parser.StartElementHandler = ret.enter_tag parser.EndElementHandler = ret.leave_tag parser.CharacterDataHandler = ret.enter_chars parser.CommentHandler = ret.enter_comment with open(fname, "rb") as fp: parser.ParseFile(fp) return ret
def _expatParseFile(self, pathOrFile): parser = ParserCreate() parser.StartElementHandler = self.startElementHandler parser.EndElementHandler = self.endElementHandler parser.CharacterDataHandler = self.characterDataHandler if isinstance(pathOrFile, (bytes, basestring)): with open(pathOrFile) as f: parser.ParseFile(f) else: parser.ParseFile(pathOrFile) return self.getRoot()
class OSMXMLParser(GeocodingParser): ID_ADDR = "OSM_ID" def __init__(self): super(OSMXMLParser, self).__init__() self.xml_parser = ParserCreate() self.xml_parser.StartElementHandler = self.xml_start_element self.xml_parser.EndElementHandler = self.xml_stop_element self.current = None self.nodes = {} # id -> (lon, lat, tags) self.buildings = [] # {points: ...} def xml_start_element(self, name, attrs): if name in ("node", "way"): self.current = { "name": name, "attrs": attrs, "tags": {}, "nodes": [] } elif name == "tag": self.current["tags"][attrs["k"]] = attrs["v"] elif name == "nd": self.current["nodes"] += [attrs["ref"]] def xml_stop_element(self, name): if name == "node": a = self.current["attrs"] self.nodes[a["id"]] = (a["lon"], a["lat"], self.current["tags"]) elif name == "way": t = self.current["tags"] if t.get("building"): # Get address fp = [ t.get("addr:country"), t.get("addr:city"), t.get("addr:street"), t.get("addr:housenumber") ] addr = ", ".join(x for x in fp if x) # Get points points = [ (float(self.nodes[n][0]), float(self.nodes[n][1])) for n in self.current["nodes"] ] self.feed_building(self.current["attrs"]["id"], addr, self.get_centroid(points)) def parse(self, f): self.xml_parser.ParseFile(f)
def __init__(self, umlFrame: UmlClassDiagramsFrame): """ In order for all of this to work you actually have to instantiate the class in order to get the 'class' variables initialized; Failure to do so will cause an ugly mess Also, I use the pycharm noinspection pragma because I cannot get the correct type imported for the parser; I think because the code is 'generated' with some kind of C language binder; """ self.logger: Logger = getLogger(__name__) DTDParser.klsLogger = self.logger self.dtdParser = ParserCreate() DTDParser.classParser = self.dtdParser self._umlFrame: UmlClassDiagramsFrame = umlFrame self.classTree: Dict[str, ElementTreeData] = {} self.dtdParser.StartDoctypeDeclHandler = DTDParser.startDocTypeHandler self.dtdParser.ElementDeclHandler = DTDParser.elementHandler self.dtdParser.AttlistDeclHandler = DTDParser.attributeListHandler self.dtdParser.EndDoctypeDeclHandler = self.endDocTypeHandler # DTDReader.endDocTypeHandler
def handlerWeather(xml): handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml) #print(result_Weather) return result_Weather
def generate_prompt(self, prompt_type): self._prompt_result = '' self._skip_this_data = False # This is set to True by <if_...> tags if the condition is False, and tells _handle_text to ignore the stuff parser = ParserCreate() parser.StartElementHandler = self._handle_start_tag parser.EndElementHandler = self._handle_end_tag parser.CharacterDataHandler = self._handle_text parser.Parse(f'<prompt>{self.config[prompt_type]}</prompt>', True) return self._prompt_result
def parse_xml(xml): from xml.parsers.expat import ParserCreate handler = SAXParseHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml)
def parseXML(xml_str): print(xml_str) handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml_str) return weather_dict
def process(self, in_stream): from xml.parsers.expat import ParserCreate parser = ParserCreate() parser.StartElementHandler = self.start_element parser.EndElementHandler = self.end_element parser.CharacterDataHandler = self.char_data parser.ParseFile(in_stream) self.p('\n')
def parse(self, xmldata): from xml.parsers.expat import ParserCreate p = ParserCreate('utf-8') p.StartElementHandler = self.start_element p.EndElementHandler = self.end_element p.CharacterDataHandler = self.char_data p.Parse(xmldata) return '\n'.join(self.output)
def parseXml(xml_str): handler = WeatherSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml_str) print(handler.forecast) return {'city': handler.city, 'forecast': handler.forecast}
def parseXml(xml_str): # print(xml_str) parser = ParserCreate(encoding='utf-8') handler = WeatherHandler() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml_str) return handler.toJSON()
def parseXml(xml): datas.clear() default1 = DefaultSaxHandler1() parser1 = ParserCreate() parser1.StartElementHandler = default1.start_xml parser1.EndElementHandler = default1.end_xml parser1.CharacterDataHandler = default1.sax_data parser1.Parse(xml) return datas
def fetch(city): weather = get_yahoo_weather(city) parser = ParserCreate() handler = WeatherSaxHandler() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(weather) print(handler.dict)
def parseXml(xml_str): #print(xml_str) handler = MySAXHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml_str) return handler.my_dict
def parse_weather(xml): handler = DefaultSaxHandler() parser = ParserCreate() parser.StartElementHandler = handler.start_element parser.EndElementHandler = handler.end_element parser.CharacterDataHandler = handler.char_data parser.Parse(xml) print(all_mes[0]) print(all_mes[1])
def parse_weather(xml): weather_parser = ParserCreate() weather_handler = WeatherParserHandler() weather_parser.StartElementHandler = weather_handler.start_element weather_parser.EndElementHandler = weather_handler.end_element weather_parser.CharacterDataHandler = weather_handler.data weather_parser.Parse(xml) return weather_handler return json.dumps(weather_handler)