@@ -61,11 +61,17 @@ def startElementNS(self, name, tagName , attrs):
6161 tagName = prefix + ":" + localname
6262 else :
6363 tagName = localname
64- node = self .document .createElementNS (uri , tagName )
64+ if self .document :
65+ node = self .document .createElementNS (uri , tagName )
66+ else :
67+ node = self .buildDocument (uri , tagName )
6568 else :
6669 # When the tagname is not prefixed, it just appears as
6770 # localname
68- node = self .document .createElement (localname )
71+ if self .document :
72+ node = self .document .createElement (localname )
73+ else :
74+ node = self .buildDocument (None , localname )
6975
7076 for aname ,value in attrs .items ():
7177 a_uri , a_localname = aname
@@ -90,7 +96,10 @@ def endElementNS(self, name, tagName):
9096 self .lastEvent = self .lastEvent [1 ]
9197
9298 def startElement (self , name , attrs ):
93- node = self .document .createElement (name )
99+ if self .document :
100+ node = self .document .createElement (name )
101+ else :
102+ node = self .buildDocument (None , name )
94103
95104 for aname ,value in attrs .items ():
96105 attr = self .document .createAttribute (aname )
@@ -127,23 +136,28 @@ def characters(self, chars):
127136 self .lastEvent = self .lastEvent [1 ]
128137
129138 def startDocument (self ):
130- publicId = systemId = None
131- if self ._locator :
132- publicId = self ._locator .getPublicId ()
133- systemId = self ._locator .getSystemId ()
134139 if self .documentFactory is None :
135140 import xml .dom .minidom
136141 self .documentFactory = xml .dom .minidom .Document .implementation
137- node = self .documentFactory .createDocument (None , publicId , systemId )
142+
143+ def buildDocument (self , uri , tagname ):
144+ # Can't do that in startDocument, since we need the tagname
145+ # XXX: obtain DocumentType
146+ node = self .documentFactory .createDocument (uri , tagname , None )
138147 self .document = node
139148 self .lastEvent [1 ] = [(START_DOCUMENT , node ), None ]
140149 self .lastEvent = self .lastEvent [1 ]
141150 self .push (node )
151+ return node .firstChild
142152
143153 def endDocument (self ):
144154 self .lastEvent [1 ] = [(END_DOCUMENT , self .document ), None ]
145155 self .pop ()
146156
157+ def clear (self ):
158+ "clear(): Explicitly release parsing structures"
159+ self .document = None
160+
147161class ErrorHandler :
148162 def warning (self , exception ):
149163 print exception
@@ -199,6 +213,13 @@ def getEvent(self):
199213 self .pulldom .firstEvent [1 ] = self .pulldom .firstEvent [1 ][1 ]
200214 return rc
201215
216+ def clear (self ):
217+ "clear(): Explicitly release parsing objects"
218+ self .pulldom .clear ()
219+ del self .pulldom
220+ self .parser = None
221+ self .stream = None
222+
202223class SAX2DOM (PullDOM ):
203224
204225 def startElementNS (self , name , tagName , attrs ):
0 commit comments