rads.xml.lxml module

XML tools using the lxml library.

rads.xml.lxml.ParseError
rads.xml.lxml.XMLParser
class rads.xml.lxml.Element(element: lxml.etree._Element, *, file: Optional[str] = None)[source]

Bases: rads.xml.base.Element

XML element that encapsulates an element from lxml.

Supports line number examination.

Param

XML element from the lxml library.

Parameters

file – Optional filename/path the element is from.

next() → rads.xml.lxml.Element[source]

Get the next sibling element.

Returns

Next XML sibling element.

Raises

StopIteration – If there is no next sibling element.

prev() → rads.xml.lxml.Element[source]

Get the previous sibling element.

Returns

Previous XML sibling element.

Raises

StopIteration – If there is no previous sibling element.

up() → rads.xml.lxml.Element[source]

Get the parent of this element.

Returns

Parent XML element.

Raises

StopIteration – If there is no parent element.

down() → rads.xml.lxml.Element[source]

Get the first child of this element.

Returns

First child XML element.

Raises

StopIteration – If this element does not have any children.

property file

Get the name of the XML file containing this element.

Returns

Name of the file containing this element, or None.

property opening_line

Get the opening line of the XML element.

Returns

Opening line number, or None.

num_lines() → int[source]

Get the number of lines making up the XML element.

Returns

Number of lines in XML element, or None.

closing_line() → int[source]

Get the closing line of the XML element.

Returns

Closing line number, or None.

property tag

Tag name of the element.

property text

Internal text of the element.

property attributes

The attributes of the element, as a dictionary.

rads.xml.lxml.parse(source: Union[str, bytes, int, IO[Any]], parser: Optional[lxml.etree.XMLParser] = None) → lxml.etree._ElementTree[source]

Parse XML document into element tree.

This is wrapper around lxml.etree.parse() to make it behave like xml.etree.ElementTree.parse().

Parameters
  • source – Filename or file object containing XML data.

  • parser – Optional parser instance, defaulting to lxml.etree.ETCompatXMLParser.

Returns

An ElementTree instance.

rads.xml.lxml.fromstring(text: Union[bytes, str], parser: Optional[lxml.etree.XMLParser] = None) → lxml.etree._Element[source]

Parse XML document from string constant.

This function can be used to embed ‘XML Literals’ in Python code.

This is wrapper around lxml.etree.fromstring() to make it behave like xml.etree.ElementTree.fromtstring().

Parameters
  • text – A string containing XML data.

  • parser – Optional parser instance, defaulting to lxml.etree.ETCompatXMLParser.

Returns

An Element instance.

rads.xml.lxml.fromstringlist(sequence: Sequence[Union[bytes, str]], parser: Optional[lxml.etree.XMLParser] = None) → lxml.etree._Element[source]

Parse XML document from sequence of string fragments.

Parameters
  • sequence – A list or other sequence of strings containing XML data.

  • parser – Optional parser instance, defaulting to lxml.etree.ETCompatXMLParser.

Returns

An Element instance.

rads.xml.lxml.error_with_file(error: lxml.etree.ParseError, file: str) → lxml.etree.ParseError[source]

Add filename to an XML parse error.

Parameters
  • error – Original XML parse error.

  • file – Filename to add.

Returns

A new parse error (of the same type as error) with the filename added.