Created
January 20, 2010 02:40
-
-
Save weaver/281539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## An example of using lxml as a namespace-aware, event-based push | |
## parser. | |
import urllib2 | |
from contextlib import closing | |
from lxml import etree | |
class Target(object): | |
def start(self, tag, attr): | |
print 'start', tag, attr | |
def end(self, tag): | |
print 'end', tag | |
def data(self, data): | |
print 'data', repr(data) | |
def close(self): | |
print 'close' | |
with closing(etree.XMLParser(target=Target())) as parser: | |
## Simulate pushing data into the parser. Normally this would be | |
## received through a network socket. | |
parser.feed('<stream:stream xmlns="jabber:client" ') | |
parser.feed('xmlns:stream="http://etherx.jabber.org/streams" version="1.0" ') | |
parser.feed('to="example.com">') | |
parser.feed('<message from="[email protected]" to="[email protected]">') | |
parser.feed('<body>Art though not Romeo, and a Montague?</body></message>') | |
parser.feed('</stream:stream>') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment