Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes
xml.etree.ElementTree.XMLParser Class Reference

Element structure builder for XML source data, based on the expat parser. More...

List of all members.

Public Member Functions

def __init__
def doctype
 (Deprecated) Handles a doctype declaration.
def feed
 Feeds data to the parser.
def close
 Finishes feeding data to the parser.

Public Attributes

 parser
 target
 entity
 version

Private Member Functions

def _raiseerror
def _fixname
def _start
def _start_list
def _data
def _end
def _comment
def _pi
def _default

Private Attributes

 _parser
 _target
 _error
 _names
 _doctype

Static Private Attributes

 __doctype = doctype

Detailed Description

Element structure builder for XML source data, based on the expat parser.

target Target object. If omitted, the builder uses an instance of the standard TreeBuilder class. html Predefine HTML entities. This flag is not supported by the current implementation. encoding Optional encoding. If given, the value overrides the encoding specified in the XML file.

See also:
#ElementTree
#TreeBuilder

Definition at line 1484 of file ElementTree.py.


Constructor & Destructor Documentation

def xml.etree.ElementTree.XMLParser.__init__ (   self,
  html = 0,
  target = None,
  encoding = None 
)

Definition at line 1486 of file ElementTree.py.

01486 
01487     def __init__(self, html=0, target=None, encoding=None):
01488         try:
01489             from xml.parsers import expat
01490         except ImportError:
01491             try:
01492                 import pyexpat as expat
01493             except ImportError:
01494                 raise ImportError(
01495                     "No module named expat; use SimpleXMLTreeBuilder instead"
01496                     )
01497         parser = expat.ParserCreate(encoding, "}")
01498         if target is None:
01499             target = TreeBuilder()
01500         # underscored names are provided for compatibility only
01501         self.parser = self._parser = parser
01502         self.target = self._target = target
01503         self._error = expat.error
01504         self._names = {} # name memo cache
01505         # callbacks
01506         parser.DefaultHandlerExpand = self._default
01507         parser.StartElementHandler = self._start
01508         parser.EndElementHandler = self._end
01509         parser.CharacterDataHandler = self._data
01510         # optional callbacks
01511         parser.CommentHandler = self._comment
01512         parser.ProcessingInstructionHandler = self._pi
01513         # let expat do the buffering, if supported
01514         try:
01515             self._parser.buffer_text = 1
01516         except AttributeError:
01517             pass
01518         # use new-style attribute handling, if supported
01519         try:
01520             self._parser.ordered_attributes = 1
01521             self._parser.specified_attributes = 1
01522             parser.StartElementHandler = self._start_list
01523         except AttributeError:
01524             pass
01525         self._doctype = None
01526         self.entity = {}
01527         try:
01528             self.version = "Expat %d.%d.%d" % expat.version_info
01529         except AttributeError:
01530             pass # unknown

Here is the caller graph for this function:


Member Function Documentation

def xml.etree.ElementTree.XMLParser._comment (   self,
  data 
) [private]

Definition at line 1571 of file ElementTree.py.

01571 
01572     def _comment(self, data):
01573         try:
01574             comment = self.target.comment
01575         except AttributeError:
01576             pass
01577         else:
01578             return comment(data)

def xml.etree.ElementTree.XMLParser._data (   self,
  text 
) [private]

Definition at line 1565 of file ElementTree.py.

01565 
01566     def _data(self, text):
01567         return self.target.data(text)

def xml.etree.ElementTree.XMLParser._default (   self,
  text 
) [private]

Definition at line 1587 of file ElementTree.py.

01587 
01588     def _default(self, text):
01589         prefix = text[:1]
01590         if prefix == "&":
01591             # deal with undefined entities
01592             try:
01593                 self.target.data(self.entity[text[1:-1]])
01594             except KeyError:
01595                 from xml.parsers import expat
01596                 err = expat.error(
01597                     "undefined entity %s: line %d, column %d" %
01598                     (text, self._parser.ErrorLineNumber,
01599                     self._parser.ErrorColumnNumber)
01600                     )
01601                 err.code = 11 # XML_ERROR_UNDEFINED_ENTITY
01602                 err.lineno = self._parser.ErrorLineNumber
01603                 err.offset = self._parser.ErrorColumnNumber
01604                 raise err
01605         elif prefix == "<" and text[:9] == "<!DOCTYPE":
01606             self._doctype = [] # inside a doctype declaration
01607         elif self._doctype is not None:
01608             # parse doctype contents
01609             if prefix == ">":
01610                 self._doctype = None
01611                 return
01612             text = text.strip()
01613             if not text:
01614                 return
01615             self._doctype.append(text)
01616             n = len(self._doctype)
01617             if n > 2:
01618                 type = self._doctype[1]
01619                 if type == "PUBLIC" and n == 4:
01620                     name, type, pubid, system = self._doctype
01621                 elif type == "SYSTEM" and n == 3:
01622                     name, type, system = self._doctype
01623                     pubid = None
01624                 else:
01625                     return
01626                 if pubid:
01627                     pubid = pubid[1:-1]
01628                 if hasattr(self.target, "doctype"):
01629                     self.target.doctype(name, pubid, system[1:-1])
01630                 elif self.doctype is not self._XMLParser__doctype:
01631                     # warn about deprecated call
01632                     self._XMLParser__doctype(name, pubid, system[1:-1])
01633                     self.doctype(name, pubid, system[1:-1])
01634                 self._doctype = None

Here is the call graph for this function:

def xml.etree.ElementTree.XMLParser._end (   self,
  tag 
) [private]

Definition at line 1568 of file ElementTree.py.

01568 
01569     def _end(self, tag):
01570         return self.target.end(self._fixname(tag))

Here is the call graph for this function:

def xml.etree.ElementTree.XMLParser._fixname (   self,
  key 
) [private]

Definition at line 1537 of file ElementTree.py.

01537 
01538     def _fixname(self, key):
01539         # expand qname, and convert name string to ascii, if possible
01540         try:
01541             name = self._names[key]
01542         except KeyError:
01543             name = key
01544             if "}" in name:
01545                 name = "{" + name
01546             self._names[key] = name
01547         return name

Here is the caller graph for this function:

def xml.etree.ElementTree.XMLParser._pi (   self,
  target,
  data 
) [private]

Definition at line 1579 of file ElementTree.py.

01579 
01580     def _pi(self, target, data):
01581         try:
01582             pi = self.target.pi
01583         except AttributeError:
01584             pass
01585         else:
01586             return pi(target, data)

def xml.etree.ElementTree.XMLParser._raiseerror (   self,
  value 
) [private]

Definition at line 1531 of file ElementTree.py.

01531 
01532     def _raiseerror(self, value):
01533         err = ParseError(value)
01534         err.code = value.code
01535         err.position = value.lineno, value.offset
01536         raise err

Here is the caller graph for this function:

def xml.etree.ElementTree.XMLParser._start (   self,
  tag,
  attrib_in 
) [private]

Definition at line 1548 of file ElementTree.py.

01548 
01549     def _start(self, tag, attrib_in):
01550         fixname = self._fixname
01551         tag = fixname(tag)
01552         attrib = {}
01553         for key, value in attrib_in.items():
01554             attrib[fixname(key)] = value
01555         return self.target.start(tag, attrib)

Here is the call graph for this function:

def xml.etree.ElementTree.XMLParser._start_list (   self,
  tag,
  attrib_in 
) [private]

Definition at line 1556 of file ElementTree.py.

01556 
01557     def _start_list(self, tag, attrib_in):
01558         fixname = self._fixname
01559         tag = fixname(tag)
01560         attrib = {}
01561         if attrib_in:
01562             for i in range(0, len(attrib_in), 2):
01563                 attrib[fixname(attrib_in[i])] = attrib_in[i+1]
01564         return self.target.start(tag, attrib)

Here is the call graph for this function:

Finishes feeding data to the parser.

Returns:
An element structure. Element

Definition at line 1670 of file ElementTree.py.

01670 
01671     def close(self):
01672         try:
01673             self._parser.Parse("", 1) # end of data
01674         except self._error as v:
01675             self._raiseerror(v)
01676         tree = self.target.close()
01677         del self.target, self._parser # get rid of circular references
01678         return tree
01679 
# compatibility

Here is the call graph for this function:

Here is the caller graph for this function:

def xml.etree.ElementTree.XMLParser.doctype (   self,
  name,
  pubid,
  system 
)

(Deprecated) Handles a doctype declaration.

Parameters:
nameDoctype name.
pubidPublic identifier.
systemSystem identifier.
This method of XMLParser is deprecated.

Definition at line 1642 of file ElementTree.py.

01642 
01643     def doctype(self, name, pubid, system):
01644         """This method of XMLParser is deprecated."""
01645         warnings.warn(
01646             "This method of XMLParser is deprecated.  Define doctype() "
01647             "method on the TreeBuilder target.",
01648             DeprecationWarning,
01649             )

Here is the call graph for this function:

Here is the caller graph for this function:

def xml.etree.ElementTree.XMLParser.feed (   self,
  data 
)

Feeds data to the parser.

Parameters:
dataEncoded data.

Definition at line 1658 of file ElementTree.py.

01658 
01659     def feed(self, data):
01660         try:
01661             self._parser.Parse(data, 0)
01662         except self._error as v:
01663             self._raiseerror(v)

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 1651 of file ElementTree.py.

Definition at line 1524 of file ElementTree.py.

Definition at line 1502 of file ElementTree.py.

Definition at line 1503 of file ElementTree.py.

Definition at line 1500 of file ElementTree.py.

Definition at line 1501 of file ElementTree.py.

Definition at line 1525 of file ElementTree.py.

Definition at line 1500 of file ElementTree.py.

Definition at line 1501 of file ElementTree.py.

Definition at line 1527 of file ElementTree.py.


The documentation for this class was generated from the following file: