Pull (streaming) XML parsing
suggest changeParsing into a struct is convenient but requires a lot of memory to hold the whole decoded document in memory.
In some cases XML files are so large that it’s not possible to decode the whole file into memory. For example XML dumps of Wikipedia content are several gigabytes in size.
Pull parsing is more efficient, but the API is harder to use.
Pull parsing requests the next token from a stream of XML tokens.
For a start tag like <person> we get an xml.StartElement token.
For an end tag like </person> we get an xml.EndElement token.
For data inside the element <person>data</person> we get an xml.CharData token.
When the decoder reaches the end, it returns the error io.EOF.
A program can print the age attribute of a <person> element and the character data inside a <city> element.
This is a very basic example. In real programs you might need to remember more state.
For example, if your XML is:
<foo>
<bar>
<foo></foo>
</bar>
</foo>
If you look just at the xml.StartElement token, you don’t know if foo is for the top-level element or is it a child of <bar> element.