gws.lib.xmlx.element

XML Element.

The Element class extends xml.etree.Element and implements the gws.core.types.IXmlElement protocol.

Source code: gws.lib.xmlx.element

Module Contents

class gws.lib.xmlx.element.XmlElementImpl(tag, attrib=None, **extra)

Bases: xml.etree.ElementTree.Element, gws.XmlElement

An XML element.

This class is the reference implementation of the Element interface.

An element’s length is its number of subelements. That means if you want to check if an element is truly empty, you should check BOTH its length AND its text attribute.

The element tag, attribute names, and attribute values can be either bytes or strings.

tag is the element name. attrib is an optional dictionary containing element attributes. extra are additional element attributes given as keyword arguments.

Example form:

<tag attrib>text<child/>…</tag>tail

find(path, namespaces=None)

Finds first matching element by tag name or path.

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

Finds text for first matching element by name or path.

findall(path, namespaces=None)

Finds all matching subelements by name or path.

iterfind(path, namespaces=None)

Returns an iterable of all matching subelements by name or path.

get(key, default=None)

Returns the value to a given key.

iter(tag=None)

Creates a tree iterator.

to_dict() dict

Creates a dictionary from an XmlElementImpl object.

Returns:

A dict with the keys tag, attrib, text, tail, tail, children.

to_string(compact_whitespace: bool = False, remove_namespaces: bool = False, with_namespace_declarations: bool = False, with_schema_locations: bool = False, with_xml_declaration: bool = False) str

Converts the XmlElement object to a string.

Parameters:
  • compact_whitespace – String will not contain any whitespaces outside of tags and elements.

  • remove_namespaces – String will not contain namespaces.

  • with_namespace_declarations – String will keep the namespace declarations.

  • with_schema_locations – String will keep the schema locations.

  • with_xml_declaration – String will keep the xml declaration.

Returns:

A String containing the xml structure.

add(tag: str, attrib: dict = None, **extra) XmlElementImpl

Creates a new XmlElementImpl and adds it as a child.

Parameters:
  • tag – XML tag.

  • attrib – XML attributes {key, value}.

Returns:

A XmlElementImpl.

attr(key: str, default=None) str

Finds the value for a given key in the XmlElementImpl.

Parameters:
  • key – Key of the attribute.

  • default – The default return.

Returns:

The vale of the key, If the key is not found the default is returned.

children() [XmlElementImpl]

Returns the children of the current XmlElementImpl.

findfirst(*paths: str) XmlElementImpl

Returns the first element in the current element.

Parameters:

paths – Path as tag/tag2/tag3 to the Element to search in.

Returns:

Returns the first found element.

textof(*paths: str) str

Returns the text of a given child-element.

Parameters:

paths – Path as tag/tag2/tag3 to the Element.

Returns:

The text of the element.

textlist(*paths: str, deep: bool = False) [XmlElementImpl]

Collects texts from child-elements.

Parameters:
  • paths – Path as tag/tag2/tag3 to the Element to collect texts from.

  • deep – If False it only looks into direct children, otherwise it searches for texts in the complete children-tree.

Returns:

A list containing all the text from the child-elements.

textdict(*paths: str, deep: bool = False) dict

Collects texts from child-elements.

Parameters:
  • paths – Path as tag/tag2/tag3 to the Element to collect texts from.

  • deep – If False it only looks into direct children, otherwise it searches for texts in the complete children-tree.

Returns:

A dict containing all the text from the child-elements.