1 """A quick DOM implementation.
3 Python's xml.dom is very slow. The xml.sax module is also slow (as it imports urllib2).
4 This is our light-weight version.
7 # Copyright (C) 2006, Thomas Leonard
8 # See the README file for details, or visit http://0install.net.
10 from xml
.parsers
import expat
13 class Element(object):
14 __slots__
= ['uri', 'name', 'attrs', 'childNodes', 'content']
15 def __init__(self
, uri
, name
, attrs
):
18 self
.attrs
= attrs
.copy()
22 attrs
= [n
+ '=' + self
.attrs
[n
] for n
in self
.attrs
]
23 start
= '<{%s}%s %s' % (self
.uri
, self
.name
, ' '.join(attrs
))
25 return start
+ '>' + '\n'.join(map(str, self
.childNodes
)) + ('</%s>' % (self
.name
))
27 return start
+ '>' + self
.content
+ ('</%s>' % (self
.name
))
31 def getAttribute(self
, name
):
32 return self
.attrs
.get(name
, None)
38 def startElementNS(self
, fullname
, attrs
):
39 split
= fullname
.split(' ', 1)
41 self
.stack
.append(Element(split
[0], split
[1], attrs
))
43 self
.stack
.append(Element(None, fullname
, attrs
))
46 def characters(self
, data
):
49 def endElementNS(self
, name
):
50 contents
= self
.contents
.strip()
51 self
.stack
[-1].content
= contents
53 new
= self
.stack
.pop()
55 self
.stack
[-1].childNodes
.append(new
)
60 handler
= QSAXhandler()
61 parser
= expat
.ParserCreate(namespace_separator
= ' ')
63 parser
.StartElementHandler
= handler
.startElementNS
64 parser
.EndElementHandler
= handler
.endElementNS
65 parser
.CharacterDataHandler
= handler
.characters
67 parser
.ParseFile(source
)