Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Some XML parsers allow incremental parsing of XML documents that is the parser can start working on the document without needing it to be fully loaded. The XMLTreeBuilder from the xml.etree.ElementTree module in the Python standard library is one such parser: <a href="http://docs.python.org/library/xml.etree.elementtree.html#xmltreebuilder-objects" rel="nofollow noreferrer">Element Tree</a></p> <p>As you can see in the example below you can feed data to the parser bit by bit as you read it from your input source. The appropriate hook methods in your handler class will get called when various XML "events" happen (tag started, tag data read, tag ended) allowing you to process the data as the XML document is loaded:</p> <pre><code>from xml.etree.ElementTree import XMLTreeBuilder class MyHandler(object): def start(self, tag, attrib): # Called for each opening tag. print tag + " started" def end(self, tag): # Called for each closing tag. print tag + " ended" def data(self, data): # Called when data is read from a tag print data + " data read" def close(self): # Called when all data has been parsed. print "All data read" handler = MyHandler() parser = XMLTreeBuilder(target=handler) parser.feed(&lt;sometag&gt;) parser.feed(&lt;sometag-child-tag&gt;text) parser.feed(&lt;/sometag-child-tag&gt;) parser.feed(&lt;/sometag&gt;) parser.close() </code></pre> <p>In this example the handler would receive five events and print:</p> <p>sometag started</p> <p>sometag-child started</p> <p>"text" data read</p> <p>sometag-child ended</p> <p>sometag ended</p> <p>All data read</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload