def handler(tag, attrib_in, event=event_name, append=append,
append((event, start(tag, attrib_in)))
parser.StartElementHandler = handler
elif event_name == "end":
def handler(tag, event=event_name, append=append,
append((event, end(tag)))
parser.EndElementHandler = handler
elif event_name == "start-ns":
def handler(prefix, uri, event=event_name, append=append):
append((event, (prefix or "", uri or "")))
parser.StartNamespaceDeclHandler = handler
elif event_name == "end-ns":
def handler(prefix, event=event_name, append=append):
parser.EndNamespaceDeclHandler = handler
raise ValueError("unknown event %r" % event_name)
def _raiseerror(self, value):
err.position = value.lineno, value.offset
# expand qname, and convert name string to ascii, if possible
def _start(self, tag, attr_list):
# Handler for expat's StartElementHandler. Since ordered_attributes
# is set, the attributes are reported as a list of alternating
for i in range(0, len(attr_list), 2):
attrib[fixname(attr_list[i])] = attr_list[i+1]
return self.target.start(tag, attrib)
return self.target.end(self._fixname(tag))
def _default(self, text):
# deal with undefined entities
data_handler = self.target.data
data_handler(self.entity[text[1:-1]])
from xml.parsers import expat
"undefined entity %s: line %d, column %d" %
(text, self.parser.ErrorLineNumber,
self.parser.ErrorColumnNumber)
err.code = 11 # XML_ERROR_UNDEFINED_ENTITY
err.lineno = self.parser.ErrorLineNumber
err.offset = self.parser.ErrorColumnNumber
elif prefix == "<" and text[:9] == "<!DOCTYPE":
self._doctype = [] # inside a doctype declaration
elif self._doctype is not None:
self._doctype.append(text)
if type == "PUBLIC" and n == 4:
name, type, pubid, system = self._doctype
elif type == "SYSTEM" and n == 3:
name, type, system = self._doctype
if hasattr(self.target, "doctype"):
self.target.doctype(name, pubid, system[1:-1])
elif self.doctype != self._XMLParser__doctype:
# warn about deprecated call
self._XMLParser__doctype(name, pubid, system[1:-1])
self.doctype(name, pubid, system[1:-1])
def doctype(self, name, pubid, system):
"""(Deprecated) Handle doctype declaration
*name* is the Doctype name, *pubid* is the public identifier,
and *system* is the system identifier.
"This method of XMLParser is deprecated. Define doctype() "
"method on the TreeBuilder target.",
# sentinel, if doctype is redefined in a subclass
"""Feed encoded data to parser."""
self.parser.Parse(data, 0)
"""Finish feeding data to parser and return element structure."""
self.parser.Parse("", 1) # end of data
close_handler = self.target.close
# get rid of circular references
del self.parser, self._parser
del self.target, self._target
# Import the C accelerators
# Element is going to be shadowed by the C implementation. We need to keep
# the Python version of it accessible for some "creative" by external code
# Element, SubElement, ParseError, TreeBuilder, XMLParser
from _elementtree import *