Skip to content

Comment on ChatGPT is really good at roleplayingparent

Comments

To parse xml without loading it into memory, one interesting element at a time in Python:

  import xml.etree.cElementTree as etree

  def getelements(filename_or_file, tag):
    context = iter(etree.iterparse(filename_or_file, events=('start', 'end')))
    _, root = next(context) # get root element
    for event, elem in context:
        if event == 'end' and elem.tag == tag:
            yield elem
            root.clear() # preserve memory
https://stackoverflow.com/questions/7697710/python-running-o...

The usage is simple: getelements() generates the desired elements one by one. Found using google search for "xml memory iterparse"

Yeah, that's an option I'm aware of, but it gets quite ugly when the rules are more than just a few.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.