Package gmisclib :: Module xmlmisc :: Class loc_finder
[frames] | no frames]

Class loc_finder

source code


A class for specifying a path through an XML tree to a particular node. Basically, this is a way of referring to a particular element that will survive serialization of the XML tree.

Instance Methods
 
__init__(self, root_element)
Create an instance of the loc_finder class and initialize it from a portion of an XML tree.
source code
elTree.Element
find_up(self, el, tag)
Search upwards (towards the document root) to find the first tag of the specified type.
source code
tuple(int)
path(self, el)
Describe the path through an XML file to a specific element.
source code

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

Static Methods
elTree.Element
walkto(elem, path)
If you have an element in an elementTree and a path obtained from loc_finder(elem).
source code
Properties

Inherited from object: __class__

Method Details

__init__(self, root_element)
(Constructor)

source code 

Create an instance of the loc_finder class and initialize it from a portion of an XML tree.

Parameters:
  • root_element (elTree.Element.) - the top element of the tree (or subtree). All positions will be computed relative to this element.
Overrides: object.__init__
Notes:
  • This walks the entire XML file and builds a cache, so it is expensive to initialize, but fairly cheap to use.
  • this works on elTree.Element object, not on an elTree.ElementTree object. If you have x which is a elTree.ElementTree, then use loc_finder(x.get_root()).

find_up(self, el, tag)

source code 

Search upwards (towards the document root) to find the first tag of the specified type.

Parameters:
  • tag (str) - the tag of an XML element.
Returns: elTree.Element
The smallest enclosing element that has the specified tag.

path(self, el)

source code 

Describe the path through an XML file to a specific element.

Parameters:
  • el (elTree.Element) - The target element.
Returns: tuple(int)
A tuple, intended to be handed to walkto.

walkto(elem, path)
Static Method

source code 

If you have an element in an elementTree and a path obtained from loc_finder(elem).path(X), this will walk you back to element X.

Parameters:
  • elem (elTree.Element) - an element in an XML tree. Normally, this is the root element.
  • path (tuple(int)) - a location in the specified XML (sub)tree.
Returns: elTree.Element
The element specified by the path, in the (sub)tree.