Skip to content

gh-63102: Add XMLPullTarget and XMLPullParser.expand() - #157439

Open
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:gh-63102-xmlpulltarget-elements
Open

gh-63102: Add XMLPullTarget and XMLPullParser.expand()#157439
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:gh-63102-xmlpulltarget-elements

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

XMLPullTarget is a parser target for XMLPullParser and iterparse() which reports elements without building a tree. start() returns a new Element with the tag and the attributes, end() returns the same object with its text, but the children are never attached: they are reported as their own events. Nothing is kept, so a document of any size can be parsed with a constant amount of memory, like with xml.dom.pulldom, but much faster (on a 500 000-row document: 0.28 s and 386 KiB, versus 2.2 s and 10 MiB for pulldom, and 1.7 MiB for TreeBuilder with the clear() idiom).

XMLPullParser.expand(element) is the counterpart of pulldom's expandNode(): it builds the subtree of a reported element from the events which are already read from the parser, and returns it. ValueError is raised if the element is not complete yet. The iterator returned by iterparse() has the same method, which reads more data itself.

Follow-up of #156888.

XMLPullTarget is a parser target for XMLPullParser and iterparse() which
reports elements without building a tree.  The reported object is an Element
with the tag, the attributes and, for the "end" event, the text, but without
children: they are reported as their own events.  Nothing is kept, so a
document of any size can be parsed with a constant amount of memory, like
with xml.dom.pulldom, but much faster.

XMLPullParser.expand() builds the subtree of a reported element from the
events which are already read from the parser, like expandNode() in pulldom.
The iterator returned by iterparse() has the same method which reads more
data if needed.
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34535474 | 📁 Comparing 72acea5 against main (3308360)

  🔍 Preview build  

3 files changed
± library/xml.etree.elementtree.html
± whatsnew/3.16.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant