Package xmlschema_acue :: Module resources :: Class XMLResource

Class XMLResource

source code


XML resource reader based on ElementTree and urllib.

:param source: a string containing the XML document or file path or an URL or a file like object or an ElementTree or an Element. :param base_url: is an optional base URL, used for the normalization of relative paths when the URL of the resource can't be obtained from the source argument. :param defuse: set the usage of SafeXMLParser for XML data. Can be 'always', 'remote' or 'never'. Default is 'remote' that uses the defusedxml only when loading remote data. :param timeout: the timeout in seconds for the connection attempt in case of remote data. :param lazy: if set to `False` the source is fully loaded into and processed from memory. Default is `True`.

Instance Methods
 
__init__(self, source, base_url=None, defuse='remote', timeout=300, lazy=True) source code
 
__unicode__(self) source code
 
__str__(self) source code
 
__repr__(self) source code
 
__setattr__(self, name, value) source code
 
tostring(self, indent='', max_lines=None, spaces_for_tab=4, xml_declaration=False)
Generates a string representation of the XML resource.
source code
 
open(self)
Returns a opened resource reader object for the instance URL.
source code
 
is_lazy(self)
Gets `True` the XML resource is lazy.
source code
 
is_loaded(self)
Gets `True` the XML text of the data source is loaded.
source code
 
iter(self, tag=None)
XML resource tree elements lazy iterator.
source code
 
iter_location_hints(self)
Yields schema location hints from the XML tree.
source code
Method Details

root(self)

source code 

The XML tree root Element.

Decorators:
  • @property

document(self)

source code 

The ElementTree document, `None` if the instance is lazy or is not created from another document or from an URL.

Decorators:
  • @property

text(self)

source code 

The XML text source, `None` if it's not available.

Decorators:
  • @property

url(self)

source code 

The source URL, `None` if the instance is created from an Element tree or from a string.

Decorators:
  • @property

base_url(self)

source code 

The base URL for completing relative locations.

Decorators:
  • @property

namespace(self)

source code 

The namespace of the XML document.

Decorators:
  • @property

defusing(source)
Static Method

source code 

Defuse an XML source, raising an `ElementTree.ParseError` if the source contains entity definitions or remote entity loading.

:param source: a filename or file object containing XML data.

Decorators:

parse(self, source)

source code 

An equivalent of *ElementTree.parse()* that can protect from XML entities attacks. When protection is applied XML data are loaded and defused before building the ElementTree instance.

:param source: a filename or file object containing XML data. :returns: an ElementTree instance.

Decorators:

iterparse(self, source, events=None)

source code 

An equivalent of *ElementTree.iterparse()* that can protect from XML entities attacks. When protection is applied the iterator yields pure-Python Element instances.

:param source: a filename or file object containing XML data. :param events: a list of events to report back. If omitted, only “end” events are reported.

Decorators:

fromstring(self, text)

source code 

An equivalent of *ElementTree.fromstring()* that can protect from XML entities attacks.

:param text: a string containing XML data. :returns: the root Element instance.

Decorators:

copy(self, **kwargs)

source code 

Resource copy method. Change init parameters with keyword arguments.

Decorators:

load(self)

source code 

Loads the XML text from the data source. If the data source is an Element the source XML text can't be retrieved.

Decorators:

get_namespaces(self)

source code 

Extracts namespaces with related prefixes from the XML resource. If a duplicate prefix declaration is encountered then adds the namespace using a different prefix, but only in the case if the namespace URI is not already mapped by another prefix.

:return: A dictionary for mapping namespace prefixes to full URI.

Decorators:

get_locations(self, locations=None)

source code 

Returns a list of schema location hints. The locations are normalized using the base URL of the instance. The *locations* argument can be a dictionary or a list of namespace resources, that are inserted before the schema location hints extracted from the XML resource.

Decorators: