Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 1 | import xml.sax |
| 2 | import xml.sax.handler |
Martin v. Löwis | 011ea47 | 2000-12-28 18:43:02 +0000 | [diff] [blame] | 3 | import types |
| 4 | |
| 5 | try: |
| 6 | _StringTypes = [types.StringType, types.UnicodeType] |
| 7 | except AttributeError: |
| 8 | _StringTypes = [types.StringType] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 9 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 10 | START_ELEMENT = "START_ELEMENT" |
| 11 | END_ELEMENT = "END_ELEMENT" |
| 12 | COMMENT = "COMMENT" |
| 13 | START_DOCUMENT = "START_DOCUMENT" |
| 14 | END_DOCUMENT = "END_DOCUMENT" |
| 15 | PROCESSING_INSTRUCTION = "PROCESSING_INSTRUCTION" |
| 16 | IGNORABLE_WHITESPACE = "IGNORABLE_WHITESPACE" |
| 17 | CHARACTERS = "CHARACTERS" |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 18 | |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 19 | class PullDOM(xml.sax.ContentHandler): |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 20 | _locator = None |
| 21 | document = None |
| 22 | |
| 23 | def __init__(self, documentFactory=None): |
| 24 | self.documentFactory = documentFactory |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 25 | self.firstEvent = [None, None] |
| 26 | self.lastEvent = self.firstEvent |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 27 | self.elementStack = [] |
| 28 | self.push = self.elementStack.append |
| 29 | try: |
| 30 | self.pop = self.elementStack.pop |
| 31 | except AttributeError: |
| 32 | # use class' pop instead |
| 33 | pass |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 34 | self._ns_contexts = [{}] # contains uri -> prefix dicts |
| 35 | self._current_context = self._ns_contexts[-1] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 36 | |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 37 | def pop(self): |
| 38 | result = self.elementStack[-1] |
Martin v. Löwis | 52ce0d0 | 2001-01-27 08:47:37 +0000 | [diff] [blame] | 39 | del self.elementStack[-1] |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 40 | return result |
| 41 | |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 42 | def setDocumentLocator(self, locator): |
| 43 | self._locator = locator |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 44 | |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 45 | def startPrefixMapping(self, prefix, uri): |
| 46 | self._ns_contexts.append(self._current_context.copy()) |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 47 | self._current_context[uri] = prefix or '' |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 49 | def endPrefixMapping(self, prefix): |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 50 | self._current_context = self._ns_contexts.pop() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 51 | |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 52 | def startElementNS(self, name, tagName , attrs): |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 53 | uri, localname = name |
Martin v. Löwis | 2c8a89c | 2000-10-06 22:36:03 +0000 | [diff] [blame] | 54 | if uri: |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 55 | # When using namespaces, the reader may or may not |
| 56 | # provide us with the original name. If not, create |
| 57 | # *a* valid tagName from the current context. |
| 58 | if tagName is None: |
Martin v. Löwis | 2c8a89c | 2000-10-06 22:36:03 +0000 | [diff] [blame] | 59 | tagName = self._current_context[uri] + ":" + localname |
| 60 | node = self.document.createElementNS(uri, tagName) |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 61 | else: |
| 62 | # When the tagname is not prefixed, it just appears as |
Martin v. Löwis | 2c8a89c | 2000-10-06 22:36:03 +0000 | [diff] [blame] | 63 | # localname |
| 64 | node = self.document.createElement(localname) |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 65 | |
| 66 | for aname,value in attrs.items(): |
Martin v. Löwis | 2c8a89c | 2000-10-06 22:36:03 +0000 | [diff] [blame] | 67 | a_uri, a_localname = aname |
| 68 | if a_uri: |
| 69 | qname = self._current_context[a_uri] + ":" + a_localname |
| 70 | attr = self.document.createAttributeNS(a_uri, qname) |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 71 | else: |
Martin v. Löwis | 2c8a89c | 2000-10-06 22:36:03 +0000 | [diff] [blame] | 72 | attr = self.document.createAttribute(a_localname) |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 73 | attr.value = value |
Martin v. Löwis | 2c8a89c | 2000-10-06 22:36:03 +0000 | [diff] [blame] | 74 | node.setAttributeNode(attr) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 75 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 76 | self.lastEvent[1] = [(START_ELEMENT, node), None] |
| 77 | self.lastEvent = self.lastEvent[1] |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 78 | self.push(node) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 79 | |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 80 | def endElementNS(self, name, tagName): |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 81 | self.lastEvent[1] = [(END_ELEMENT, self.pop()), None] |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 82 | self.lastEvent = self.lastEvent[1] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 83 | |
Lars Gustäbel | d178ba6 | 2000-10-11 22:34:04 +0000 | [diff] [blame] | 84 | def startElement(self, name, attrs): |
| 85 | node = self.document.createElement(name) |
| 86 | |
| 87 | for aname,value in attrs.items(): |
| 88 | attr = self.document.createAttribute(aname) |
| 89 | attr.value = value |
| 90 | node.setAttributeNode(attr) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 91 | |
Lars Gustäbel | d178ba6 | 2000-10-11 22:34:04 +0000 | [diff] [blame] | 92 | self.lastEvent[1] = [(START_ELEMENT, node), None] |
| 93 | self.lastEvent = self.lastEvent[1] |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 94 | self.push(node) |
Lars Gustäbel | d178ba6 | 2000-10-11 22:34:04 +0000 | [diff] [blame] | 95 | |
| 96 | def endElement(self, name): |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 97 | self.lastEvent[1] = [(END_ELEMENT, self.pop()), None] |
Lars Gustäbel | d178ba6 | 2000-10-11 22:34:04 +0000 | [diff] [blame] | 98 | self.lastEvent = self.lastEvent[1] |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 99 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 100 | def comment(self, s): |
| 101 | node = self.document.createComment(s) |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 102 | self.lastEvent[1] = [(COMMENT, node), None] |
| 103 | self.lastEvent = self.lastEvent[1] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 104 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 105 | def processingInstruction(self, target, data): |
| 106 | node = self.document.createProcessingInstruction(target, data) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 107 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 108 | self.lastEvent[1] = [(PROCESSING_INSTRUCTION, node), None] |
| 109 | self.lastEvent = self.lastEvent[1] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 110 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 111 | def ignorableWhitespace(self, chars): |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 112 | node = self.document.createTextNode(chars) |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 113 | self.lastEvent[1] = [(IGNORABLE_WHITESPACE, node), None] |
| 114 | self.lastEvent = self.lastEvent[1] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 115 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 116 | def characters(self, chars): |
| 117 | node = self.document.createTextNode(chars) |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 118 | self.lastEvent[1] = [(CHARACTERS, node), None] |
| 119 | self.lastEvent = self.lastEvent[1] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 120 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 121 | def startDocument(self): |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 122 | publicId = systemId = None |
| 123 | if self._locator: |
| 124 | publicId = self._locator.getPublicId() |
| 125 | systemId = self._locator.getSystemId() |
| 126 | if self.documentFactory is None: |
| 127 | import xml.dom.minidom |
| 128 | self.documentFactory = xml.dom.minidom.Document.implementation |
| 129 | node = self.documentFactory.createDocument(None, publicId, systemId) |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 130 | self.document = node |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 131 | self.lastEvent[1] = [(START_DOCUMENT, node), None] |
| 132 | self.lastEvent = self.lastEvent[1] |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 133 | self.push(node) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 134 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 135 | def endDocument(self): |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 136 | self.lastEvent[1] = [(END_DOCUMENT, self.document), None] |
| 137 | self.pop() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 138 | |
| 139 | class ErrorHandler: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 140 | def warning(self, exception): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 141 | print exception |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 142 | def error(self, exception): |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 143 | raise exception |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 144 | def fatalError(self, exception): |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 145 | raise exception |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 146 | |
| 147 | class DOMEventStream: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 148 | def __init__(self, stream, parser, bufsize): |
| 149 | self.stream = stream |
| 150 | self.parser = parser |
| 151 | self.bufsize = bufsize |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 152 | self.reset() |
| 153 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 154 | def reset(self): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 155 | self.pulldom = PullDOM() |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 156 | # This content handler relies on namespace support |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 157 | self.parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 158 | self.parser.setContentHandler(self.pulldom) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 159 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 160 | def __getitem__(self, pos): |
| 161 | rc = self.getEvent() |
| 162 | if rc: |
| 163 | return rc |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 164 | raise IndexError |
| 165 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 166 | def expandNode(self, node): |
| 167 | event = self.getEvent() |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 168 | parents = [node] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 169 | while event: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 170 | token, cur_node = event |
| 171 | if cur_node is node: |
| 172 | return |
Lars Gustäbel | ec964d5 | 2000-10-13 20:53:27 +0000 | [diff] [blame] | 173 | if token != END_ELEMENT: |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 174 | parents[-1].appendChild(cur_node) |
| 175 | if token == START_ELEMENT: |
| 176 | parents.append(cur_node) |
| 177 | elif token == END_ELEMENT: |
| 178 | del parents[-1] |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 179 | event = self.getEvent() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 180 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 181 | def getEvent(self): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 182 | if not self.pulldom.firstEvent[1]: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 183 | self.pulldom.lastEvent = self.pulldom.firstEvent |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 184 | while not self.pulldom.firstEvent[1]: |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 185 | buf = self.stream.read(self.bufsize) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 186 | if not buf: |
Martin v. Löwis | e3fc722 | 2001-01-27 08:34:21 +0000 | [diff] [blame] | 187 | self.parser.close() |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 188 | return None |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 189 | self.parser.feed(buf) |
| 190 | rc = self.pulldom.firstEvent[1][0] |
| 191 | self.pulldom.firstEvent[1] = self.pulldom.firstEvent[1][1] |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 192 | return rc |
| 193 | |
Lars Gustäbel | ec964d5 | 2000-10-13 20:53:27 +0000 | [diff] [blame] | 194 | class SAX2DOM(PullDOM): |
| 195 | |
| 196 | def startElementNS(self, name, tagName , attrs): |
| 197 | PullDOM.startElementNS(self, name, tagName, attrs) |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 198 | curNode = self.elementStack[-1] |
| 199 | parentNode = self.elementStack[-2] |
| 200 | parentNode.appendChild(curNode) |
Lars Gustäbel | ec964d5 | 2000-10-13 20:53:27 +0000 | [diff] [blame] | 201 | |
| 202 | def startElement(self, name, attrs): |
| 203 | PullDOM.startElement(self, name, attrs) |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 204 | curNode = self.elementStack[-1] |
| 205 | parentNode = self.elementStack[-2] |
| 206 | parentNode.appendChild(curNode) |
Lars Gustäbel | ec964d5 | 2000-10-13 20:53:27 +0000 | [diff] [blame] | 207 | |
| 208 | def processingInstruction(self, target, data): |
| 209 | PullDOM.processingInstruction(self, target, data) |
| 210 | node = self.lastEvent[0][1] |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 211 | parentNode = self.elementStack[-1] |
| 212 | parentNode.appendChild(node) |
Lars Gustäbel | ec964d5 | 2000-10-13 20:53:27 +0000 | [diff] [blame] | 213 | |
| 214 | def ignorableWhitespace(self, chars): |
| 215 | PullDOM.ignorableWhitespace(self, chars) |
| 216 | node = self.lastEvent[0][1] |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 217 | parentNode = self.elementStack[-1] |
| 218 | parentNode.appendChild(node) |
Lars Gustäbel | ec964d5 | 2000-10-13 20:53:27 +0000 | [diff] [blame] | 219 | |
| 220 | def characters(self, chars): |
| 221 | PullDOM.characters(self, chars) |
| 222 | node = self.lastEvent[0][1] |
Martin v. Löwis | 04a1a54 | 2001-01-26 18:53:42 +0000 | [diff] [blame] | 223 | parentNode = self.elementStack[-1] |
| 224 | parentNode.appendChild(node) |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 225 | |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 226 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 227 | default_bufsize = (2 ** 14) - 20 |
| 228 | |
Fred Drake | c16adce | 2000-12-14 18:00:18 +0000 | [diff] [blame] | 229 | def parse(stream_or_string, parser=None, bufsize=None): |
| 230 | if bufsize is None: |
| 231 | bufsize = default_bufsize |
Martin v. Löwis | 011ea47 | 2000-12-28 18:43:02 +0000 | [diff] [blame] | 232 | if type(stream_or_string) in _StringTypes: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 233 | stream = open(stream_or_string) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 234 | else: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 235 | stream = stream_or_string |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 236 | if not parser: |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 237 | parser = xml.sax.make_parser() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 238 | return DOMEventStream(stream, parser, bufsize) |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 239 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 240 | def parseString(string, parser=None): |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 241 | try: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 242 | from cStringIO import StringIO |
Fred Drake | 55c3819 | 2000-06-29 19:39:57 +0000 | [diff] [blame] | 243 | except ImportError: |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 244 | from StringIO import StringIO |
Fred Drake | 16f6329 | 2000-10-23 18:09:50 +0000 | [diff] [blame] | 245 | |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 246 | bufsize = len(string) |
| 247 | buf = StringIO(string) |
Martin v. Löwis | a13a9dc | 2000-09-24 21:54:14 +0000 | [diff] [blame] | 248 | if not parser: |
| 249 | parser = xml.sax.make_parser() |
Fred Drake | 1f54902 | 2000-09-24 05:21:58 +0000 | [diff] [blame] | 250 | return DOMEventStream(buf, parser, bufsize) |