Package lxml :: Module etree :: Class _ElementTree
[hide private]
[frames] | no frames]

Class _ElementTree

object --+
         |
        _ElementTree
Known Subclasses:

Instance Methods [hide private]
 
__copy__(...)
 
__deepcopy__(...)
a new object with type S, a subtype of T
__new__(T, S, ...)
 
_setroot(self, root)
Relocate the ElementTree to a new root node.
 
find(self, path, namespaces=None)
Finds the first toplevel element with given tag. Same as tree.getroot().find(path).
 
findall(self, path, namespaces=None)
Finds all elements matching the ElementPath expression. Same as getroot().findall(path).
 
findtext(self, path, default=None, namespaces=None)
Finds the text for the first element matching the ElementPath expression. Same as getroot().findtext(path)
 
getelementpath(self, element)
Returns a structural, absolute ElementPath expression to find the element. This path can be used in the .find() method to look up the element, provided that the elements along the path and their list of immediate children were not modified in between.
 
getiterator(self, tag=None, *tags)
Returns a sequence or iterator of all elements in document order (depth first pre-order), starting with the root element.
 
getpath(self, element)
Returns a structural, absolute XPath expression to find the element.
 
getroot(self)
Gets the root element for this tree.
 
iter(self, tag=None, *tags)
Creates an iterator for the root element. The iterator loops over all elements in this tree, in document order. Note that siblings of the root element (comments or processing instructions) are not returned by the iterator.
 
iterfind(self, path, namespaces=None)
Iterates over all elements matching the ElementPath expression. Same as getroot().iterfind(path).
 
parse(self, source, parser=None, base_url=None)
Updates self with the content of source and returns its root
 
relaxng(self, relaxng)
Validate this document using other document.
 
write(self, file, encoding=None, method="xml", pretty_print=False, xml_declaration=None, with_tail=True, standalone=None, doctype=None, compression=0, exclusive=False, with_comments=True, inclusive_ns_prefixes=None)
Write the tree to a filename, file or file-like object.
 
write_c14n(self, file, exclusive=False, with_comments=True, compression=0, inclusive_ns_prefixes=None)
C14N write of document. Always writes UTF-8.
 
xinclude(self)
Process the XInclude nodes in this document and include the referenced XML fragments.
 
xmlschema(self, xmlschema)
Validate this document using other document.
 
xpath(self, _path, namespaces=None, extensions=None, smart_strings=True, **_variables)
XPath evaluate in context of document.
 
xslt(self, _xslt, extensions=None, access_control=None, **_kw)
Transform this document using other document.

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Properties [hide private]
  docinfo
Information about the document provided by parser and DTD.
  parser
The parser that was used to parse the document in this ElementTree.

Inherited from object: __class__

Method Details [hide private]

__new__(T, S, ...)

 
Returns: a new object with type S, a subtype of T
Overrides: object.__new__

find(self, path, namespaces=None)

 

Finds the first toplevel element with given tag. Same as tree.getroot().find(path).

The optional namespaces argument accepts a prefix-to-namespace mapping that allows the usage of XPath prefixes in the path expression.

findall(self, path, namespaces=None)

 

Finds all elements matching the ElementPath expression. Same as getroot().findall(path).

The optional namespaces argument accepts a prefix-to-namespace mapping that allows the usage of XPath prefixes in the path expression.

findtext(self, path, default=None, namespaces=None)

 

Finds the text for the first element matching the ElementPath expression. Same as getroot().findtext(path)

The optional namespaces argument accepts a prefix-to-namespace mapping that allows the usage of XPath prefixes in the path expression.

getelementpath(self, element)

 

Returns a structural, absolute ElementPath expression to find the element. This path can be used in the .find() method to look up the element, provided that the elements along the path and their list of immediate children were not modified in between.

ElementPath has the advantage over an XPath expression (as returned by the .getpath() method) that it does not require additional prefix declarations. It is always self-contained.

getiterator(self, tag=None, *tags)

 

Returns a sequence or iterator of all elements in document order (depth first pre-order), starting with the root element.

Can be restricted to find only elements with specific tags, see _Element.iter.

Deprecated: Note that this method is deprecated as of ElementTree 1.3 and lxml 2.0. It returns an iterator in lxml, which diverges from the original ElementTree behaviour. If you want an efficient iterator, use the tree.iter() method instead. You should only use this method in new code if you require backwards compatibility with older versions of lxml or ElementTree.

getpath(self, element)

 

Returns a structural, absolute XPath expression to find the element.

For namespaced elements, the expression uses prefixes from the document, which therefore need to be provided in order to make any use of the expression in XPath.

Also see the method getelementpath(self, element), which returns a self-contained ElementPath expression.

iter(self, tag=None, *tags)

 

Creates an iterator for the root element. The iterator loops over all elements in this tree, in document order. Note that siblings of the root element (comments or processing instructions) are not returned by the iterator.

Can be restricted to find only elements with specific tags, see _Element.iter.

iterfind(self, path, namespaces=None)

 

Iterates over all elements matching the ElementPath expression. Same as getroot().iterfind(path).

The optional namespaces argument accepts a prefix-to-namespace mapping that allows the usage of XPath prefixes in the path expression.

relaxng(self, relaxng)

 

Validate this document using other document.

The relaxng argument is a tree that should contain a Relax NG schema.

Returns True or False, depending on whether validation succeeded.

Note: if you are going to apply the same Relax NG schema against multiple documents, it is more efficient to use the RelaxNG class directly.

write(self, file, encoding=None, method="xml", pretty_print=False, xml_declaration=None, with_tail=True, standalone=None, doctype=None, compression=0, exclusive=False, with_comments=True, inclusive_ns_prefixes=None)

 

Write the tree to a filename, file or file-like object.

Defaults to ASCII encoding and writing a declaration as needed.

The keyword argument 'method' selects the output method: 'xml', 'html', 'text' or 'c14n'. Default is 'xml'.

The exclusive and with_comments arguments are only used with C14N output, where they request exclusive and uncommented C14N serialisation respectively.

Passing a boolean value to the standalone option will output an XML declaration with the corresponding standalone flag.

The doctype option allows passing in a plain string that will be serialised before the XML tree. Note that passing in non well-formed content here will make the XML output non well-formed. Also, an existing doctype in the document tree will not be removed when serialising an ElementTree instance.

The compression option enables GZip compression level 1-9.

The inclusive_ns_prefixes should be a list of namespace strings (i.e. ['xs', 'xsi']) that will be promoted to the top-level element during exclusive C14N serialisation. This parameter is ignored if exclusive mode=False.

If exclusive=True and no list is provided, a namespace will only be rendered if it is used by the immediate parent or one of its attributes and its prefix and values have not already been rendered by an ancestor of the namespace node's parent element.

write_c14n(self, file, exclusive=False, with_comments=True, compression=0, inclusive_ns_prefixes=None)

 

C14N write of document. Always writes UTF-8.

The compression option enables GZip compression level 1-9.

The inclusive_ns_prefixes should be a list of namespace strings (i.e. ['xs', 'xsi']) that will be promoted to the top-level element during exclusive C14N serialisation. This parameter is ignored if exclusive mode=False.

If exclusive=True and no list is provided, a namespace will only be rendered if it is used by the immediate parent or one of its attributes and its prefix and values have not already been rendered by an ancestor of the namespace node's parent element.

xinclude(self)

 

Process the XInclude nodes in this document and include the referenced XML fragments.

There is support for loading files through the file system, HTTP and FTP.

Note that XInclude does not support custom resolvers in Python space due to restrictions of libxml2 <= 2.6.29.

xmlschema(self, xmlschema)

 

Validate this document using other document.

The xmlschema argument is a tree that should contain an XML Schema.

Returns True or False, depending on whether validation succeeded.

Note: If you are going to apply the same XML Schema against multiple documents, it is more efficient to use the XMLSchema class directly.

xpath(self, _path, namespaces=None, extensions=None, smart_strings=True, **_variables)

 

XPath evaluate in context of document.

namespaces is an optional dictionary with prefix to namespace URI mappings, used by XPath. extensions defines additional extension functions.

Returns a list (nodeset), or bool, float or string.

In case of a list result, return Element for element nodes, string for text and attribute values.

Note: if you are going to apply multiple XPath expressions against the same document, it is more efficient to use XPathEvaluator directly.

xslt(self, _xslt, extensions=None, access_control=None, **_kw)

 

Transform this document using other document.

xslt is a tree that should be XSLT keyword parameters are XSLT transformation parameters.

Returns the transformed tree.

Note: if you are going to apply the same XSLT stylesheet against multiple documents, it is more efficient to use the XSLT class directly.