0

I am trying to understand the iterparse section in particular.

What are the "events" referred to here? Do the start and end events correspond to start and end tags in the elements of an XML file, and if so, what does it do?

Here is the section:xml.etree.ElementTree.iterparse(source, events=None, parser=None)

Parses an XML section into an element tree incrementally, and reports what’s going on to the user. source is a filename or file object containing XML data. events is a sequence of events to report back. The supported events are the strings "start", "end", "comment", "pi", "start-ns" and "end-ns" (the “ns” events are used to get detailed namespace information). If events is omitted, only "end" events are reported. parser is an optional parser instance. If not given, the standard XMLParser parser is used. parser must be a subclass of XMLParser and can only use the default TreeBuilder as a target. Returns an iterator providing (event, elem) pairs.

Note that while iterparse() builds the tree incrementally, it issues blocking reads on source (or the file it names). As such, it’s unsuitable for applications where blocking reads can’t be made. For fully non-blocking parsing, see XMLPullParser.

Note:iterparse() only guarantees that it has seen the “>” character of a starting tag when it emits a “start” event, so the attributes are defined, but the contents of the text and tail attributes are undefined at that point. The same applies to the element children; they may or may not be present. If you need a fully populated element, look for “end” events instead.

I haven't tried anything yet, but I want to make sure I understand how to use this function to avoid problems.

mzjn
  • 48,958
  • 13
  • 128
  • 248
ktz_he
  • 1

1 Answers1

0

What are the "events" referred to here?

The "events" are fired by the parser when it encounters new XML element, the end of XML element, etc. The code below is "intersted" in 2 events:

...
for event, elem in ElementTree.iterparse(file_obj, events=("start", "end")):
...

So the parser "calls" your code and you decide what to do with the information

balderman
  • 22,927
  • 7
  • 34
  • 52