Back to index

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

ElementTree wrapper class. More...

List of all members.

Public Member Functions

def __init__
def getroot
 Gets the root element for this tree.
def parse
 Loads an external XML document into this element tree.
def iter
 Creates a tree iterator for the root element.
def getiterator
def find
 Finds the first toplevel element with given tag.
def findtext
 Finds the element text for the first toplevel element with given tag.
def findall
 Finds all toplevel elements with the given tag.
def iterfind
 Finds all matching subelements, by tag name or path.
def write
def write_c14n

Private Member Functions

def _setroot
 Replaces the root element for this tree.

Private Attributes

 _root

Detailed Description

ElementTree wrapper class.

This class represents an entire element hierarchy, and adds some extra support for serialization to and from standard XML.

Parameters:
elementOptional root element. file Optional file handle or file name. If given, the tree is initialized with the contents of this XML file.

Definition at line 627 of file ElementTree.py.


Constructor & Destructor Documentation

def xml.etree.ElementTree.ElementTree.__init__ (   self,
  element = None,
  file = None 
)

Definition at line 629 of file ElementTree.py.

00629 
00630     def __init__(self, element=None, file=None):
00631         # assert element is None or iselement(element)
00632         self._root = element # first node
00633         if file:
00634             self.parse(file)

Here is the caller graph for this function:


Member Function Documentation

def xml.etree.ElementTree.ElementTree._setroot (   self,
  element 
) [private]

Replaces the root element for this tree.

This discards the current contents of the tree, and replaces it with the given element. Use with care.

Parameters:
elementAn element instance.

Definition at line 651 of file ElementTree.py.

00651 
00652     def _setroot(self, element):
00653         # assert iselement(element)
00654         self._root = element

def xml.etree.ElementTree.ElementTree.find (   self,
  path,
  namespaces = None 
)

Finds the first toplevel element with given tag.

Same as getroot().find(path).

Parameters:
pathWhat element to look for. namespaces Optional namespace prefix map.
Returns:
The first matching element, or None if no element was found. Element or None

Definition at line 716 of file ElementTree.py.

00716 
00717     def find(self, path, namespaces=None):
00718         # assert self._root is not None
00719         if path[:1] == "/":
00720             path = "." + path
00721             warnings.warn(
00722                 "This search is broken in 1.3 and earlier, and will be "
00723                 "fixed in a future version.  If you rely on the current "
00724                 "behaviour, change it to %r" % path,
00725                 FutureWarning, stacklevel=2
00726                 )
00727         return self._root.find(path, namespaces)

Here is the call graph for this function:

def xml.etree.ElementTree.ElementTree.findall (   self,
  path,
  namespaces = None 
)

Finds all toplevel elements with the given tag.

Same as getroot().findall(path).

Parameters:
pathWhat element to look for. namespaces Optional namespace prefix map.
Returns:
A list or iterator containing all matching elements, in document order. list of Element instances

Definition at line 763 of file ElementTree.py.

00763 
00764     def findall(self, path, namespaces=None):
00765         # assert self._root is not None
00766         if path[:1] == "/":
00767             path = "." + path
00768             warnings.warn(
00769                 "This search is broken in 1.3 and earlier, and will be "
00770                 "fixed in a future version.  If you rely on the current "
00771                 "behaviour, change it to %r" % path,
00772                 FutureWarning, stacklevel=2
00773                 )
00774         return self._root.findall(path, namespaces)

Here is the call graph for this function:

def xml.etree.ElementTree.ElementTree.findtext (   self,
  path,
  default = None,
  namespaces = None 
)

Finds the element text for the first toplevel element with given tag.

Same as getroot().findtext(path).

Parameters:
pathWhat toplevel element to look for.
defaultWhat to return if the element was not found. namespaces Optional namespace prefix map.
Returns:
The text content of the first matching element, or the default value no element was found. Note that if the element is found, but has no text content, this method returns an empty string. string

Definition at line 741 of file ElementTree.py.

00741 
00742     def findtext(self, path, default=None, namespaces=None):
00743         # assert self._root is not None
00744         if path[:1] == "/":
00745             path = "." + path
00746             warnings.warn(
00747                 "This search is broken in 1.3 and earlier, and will be "
00748                 "fixed in a future version.  If you rely on the current "
00749                 "behaviour, change it to %r" % path,
00750                 FutureWarning, stacklevel=2
00751                 )
00752         return self._root.findtext(path, default, namespaces)

Here is the call graph for this function:

def xml.etree.ElementTree.ElementTree.getiterator (   self,
  tag = None 
)

Definition at line 698 of file ElementTree.py.

00698 
00699     def getiterator(self, tag=None):
00700         # Change for a DeprecationWarning in 1.4
00701         warnings.warn(
00702             "This method will be removed in future versions.  "
00703             "Use 'tree.iter()' or 'list(tree.iter())' instead.",
00704             PendingDeprecationWarning, stacklevel=2
00705         )
00706         return list(self.iter(tag))

Here is the call graph for this function:

Gets the root element for this tree.

Returns:
An element instance. Element

Definition at line 641 of file ElementTree.py.

00641 
00642     def getroot(self):
00643         return self._root

def xml.etree.ElementTree.ElementTree.iter (   self,
  tag = None 
)

Creates a tree iterator for the root element.

The iterator loops over all elements in this tree, in document order.

Parameters:
tagWhat tags to look for (default is to return all elements)
Returns:
An iterator. iterator

Definition at line 693 of file ElementTree.py.

00693 
00694     def iter(self, tag=None):
00695         # assert self._root is not None
00696         return self._root.iter(tag)

Here is the caller graph for this function:

def xml.etree.ElementTree.ElementTree.iterfind (   self,
  path,
  namespaces = None 
)

Finds all matching subelements, by tag name or path.

Same as getroot().iterfind(path).

Parameters:
pathWhat element to look for. namespaces Optional namespace prefix map.
Returns:
An iterator or sequence containing all matching elements, in document order. a generated sequence of Element instances

Definition at line 785 of file ElementTree.py.

00785 
00786     def iterfind(self, path, namespaces=None):
00787         # assert self._root is not None
00788         if path[:1] == "/":
00789             path = "." + path
00790             warnings.warn(
00791                 "This search is broken in 1.3 and earlier, and will be "
00792                 "fixed in a future version.  If you rely on the current "
00793                 "behaviour, change it to %r" % path,
00794                 FutureWarning, stacklevel=2
00795                 )
00796         return self._root.iterfind(path, namespaces)

Here is the call graph for this function:

def xml.etree.ElementTree.ElementTree.parse (   self,
  source,
  parser = None 
)

Loads an external XML document into this element tree.

Parameters:
sourceA file name or file object. If a file object is given, it only has to implement a read(n) method. parser An optional parser instance. If not given, the standard XMLParser parser is used.
Returns:
The document root element. Element
Exceptions:
ParseErrorIf the parser fails to parse the document.

Definition at line 666 of file ElementTree.py.

00666 
00667     def parse(self, source, parser=None):
00668         close_source = False
00669         if not hasattr(source, "read"):
00670             source = open(source, "rb")
00671             close_source = True
00672         try:
00673             if not parser:
00674                 parser = XMLParser(target=TreeBuilder())
00675             while 1:
00676                 data = source.read(65536)
00677                 if not data:
00678                     break
00679                 parser.feed(data)
00680             self._root = parser.close()
00681             return self._root
00682         finally:
00683             if close_source:
00684                 source.close()

Here is the caller graph for this function:

def xml.etree.ElementTree.ElementTree.write (   self,
  file_or_filename,
  encoding = None,
  xml_declaration = None,
  default_namespace = None,
  method = None 
)

Definition at line 816 of file ElementTree.py.

00816 
00817               method=None):
00818         # assert self._root is not None
00819         if not method:
00820             method = "xml"
00821         elif method not in _serialize:
00822             # FIXME: raise an ImportError for c14n if ElementC14N is missing?
00823             raise ValueError("unknown method %r" % method)
00824         if not encoding:
00825             if method == "c14n":
00826                 encoding = "utf-8"
00827             else:
00828                 encoding = "us-ascii"
00829         elif encoding == str:  # lxml.etree compatibility.
00830             encoding = "unicode"
00831         else:
00832             encoding = encoding.lower()
00833         if hasattr(file_or_filename, "write"):
00834             file = file_or_filename
00835         else:
00836             if encoding != "unicode":
00837                 file = open(file_or_filename, "wb")
00838             else:
00839                 file = open(file_or_filename, "w")
00840         if encoding != "unicode":
00841             def write(text):
00842                 try:
00843                     return file.write(text.encode(encoding,
00844                                                   "xmlcharrefreplace"))
00845                 except (TypeError, AttributeError):
00846                     _raise_serialization_error(text)
00847         else:
00848             write = file.write
00849         if method == "xml" and (xml_declaration or
00850                 (xml_declaration is None and
00851                  encoding not in ("utf-8", "us-ascii", "unicode"))):
00852             declared_encoding = encoding
00853             if encoding == "unicode":
00854                 # Retrieve the default encoding for the xml declaration
00855                 import locale
00856                 declared_encoding = locale.getpreferredencoding()
00857             write("<?xml version='1.0' encoding='%s'?>\n" % declared_encoding)
00858         if method == "text":
00859             _serialize_text(write, self._root)
00860         else:
00861             qnames, namespaces = _namespaces(self._root, default_namespace)
00862             serialize = _serialize[method]
00863             serialize(write, self._root, qnames, namespaces)
00864         if file_or_filename is not file:
00865             file.close()

Here is the call graph for this function:

Definition at line 866 of file ElementTree.py.

00866 
00867     def write_c14n(self, file):
00868         # lxml.etree compatibility.  use output method instead
00869         return self.write(file, method="c14n")
00870 
00871 # --------------------------------------------------------------------
00872 # serialization support


Member Data Documentation

Definition at line 631 of file ElementTree.py.


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