def iterparse_until(source, target_name, quit_name): '''Iteratively parse XML stream in ``source``, yielding XML elements matching ``target_name``. If at any point a tag matching ``quit_name`` is encountered, stop parsing. Parameters ---------- source: file-like A file-like object over an XML document tag_name: str The name of the XML tag to parse until quit_name: str The name to stop parsing at. Yields ------ lxml.etree.Element ''' g = etree.iterparse(source, ('start', 'end')) for event, tag in g: if event == 'start': if xml._local_name(tag) == quit_name: break else: if xml._local_name(tag) == target_name: yield tag else: tag.clear()
def get_tag_attributes(source, tag_name, stop_at=None): '''Iteratively parse XML stream in ``source`` until encountering ``tag_name`` at which point parsing terminates and return the attributes of the matched tag. Parameters ---------- source: file-like A file-like object over an XML document tag_name: str The name of the XML tag to parse until Returns ------- dict ''' g = etree.iterparse(source, ('start', 'end')) for event, tag in g: if event == 'start': if xml._local_name(tag) == tag_name: return tag.attrib elif stop_at and xml._local_name(tag) == stop_at: break else: continue else: tag.clear() return None
def iterparse_until(source, target_name, quit_name): g = etree.iterparse(source, ('start', 'end')) for event, tag in g: if event == 'start': if xml._local_name(tag) == quit_name: break else: if xml._local_name(tag) == target_name: yield tag else: tag.clear()
def get_tag_attributes(source, tag_name): '''Iteratively parse XML stream in ``source`` until encountering ``tag_name`` at which point parsing terminates and return the attributes of the matched tag. Parameters ---------- source: file-like A file-like object over an XML document tag_name: str The name of the XML tag to parse until Returns ------- dict ''' g = etree.iterparse(source, ('start', 'end')) for event, tag in g: if event == 'start': if xml._local_name(tag) == tag_name: return tag.attrib else: continue else: tag.clear() return None
def get_tag_attributes(source, tag_name): g = etree.iterparse(source, ('start', 'end')) for event, tag in g: if event == 'start': if xml._local_name(tag) == tag_name: return tag.attrib else: continue else: tag.clear() return None