def _on_obj_ready(self, parent, cls, response): body = response.read() if response.status == 200 and body: obj = cls(parent) h = XmlHandler(obj, parent) sax_parse(body, h) return obj else: raise self._for_status(response, body)
def _on_status_ready(self, parent, response): body = response.read() if response.status == 200 and body: rs = ResultSet() h = XmlHandler(rs, parent) sax_parse(body, h) return rs.status else: raise self._for_status(response, body)
def parse(xml, schema, tmp_dict): """ xml should be a filename or a file-like object containing xml data. schema should be a Tag object defining the structure of the xml document. tmp_dict is used by parser functions in Tag objects to share data. It can be pre-populated with values. """ sax_parse(xml, SaxHandler(schema, tmp_dict))
def parse(xml, schema, tmp_dict): """ xml should be a filename or a file-like object containing xml data. schema should be a Tag object defining the structure of the xml document. tmp_dict is used by parser functions in Tag objects to share data. It can be pre-populated with values. """ if isinstance(xml, unicode): # Workaround for "Sax parser crashes if given unicode file name" bug: # http://bugs.python.org/issue11159 xml = xml.encode(sys.getfilesystemencoding()) sax_parse(xml, SaxHandler(schema, tmp_dict))
#!/usr/bin/env python # encoding: utf-8 from xml.etree.ElementTree import parse from xml.sax import parse as sax_parse # <?xml version="1.0"?> # <!DOCTYPE lolz [ # <!ENTITY lol "lol"> # <!ELEMENT lolz (#PCDATA)> # <!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;"> # <!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;"> # <!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;"> # <!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;"> # <!ENTITY lol5 "&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;&lol4;"> # <!ENTITY lol6 "&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;&lol5;"> # <!ENTITY lol7 "&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;&lol6;"> # <!ENTITY lol8 "&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;&lol7;"> # <!ENTITY lol9 "&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;&lol8;"> # ]> # <lolz>&lol9;</lolz> sax_parse("./poc.xml") # root = parse("./poc.xml")
def parse(file): dh = SignatureHandler() sax_parse(file, dh) return dh._root
def get_all_stations(file: Path = DEFAULT_GEO_PATH) -> List[OSMNode]: with file.open("rb") as stream: handler = OSMStationHandler() sax_parse(stream, handler) return handler.stations