gws.lib.xmlx.element

XmlElement implementation.

Source code: gws.lib.xmlx.element

Module Contents

class gws.lib.xmlx.element.XmlElement(tag: str, attrib: dict | None = None, **extra)

Bases: gws.XmlElement

XML Element.

Extends ElementTree.Element (https://docs.python.org/3/library/xml.etree.elementtree.html#element-objects).

attrib

Dictionary of element attributes.

lcName

Element name (tag without a namespace) in lower case.

name

Element name (tag without a namespace).

tag

Tag name, with an optional namespace in the Clark notation.

tail = ''

Text after this element’s end tag.

text = ''

Text before first subelement.

add(tag, attrib=None, **extra)

Creates a new element and adds it as a child.

Parameters:
  • tag – XML tag.

  • attrib – XML attributes {key, value}.

append(subelement)

Adds the element subelement to the end of this element’s internal list of subelements.

attr(key, default='')

Alias for ‘get’.

children()

Returns the children of the current element.

clear()

Resets an element.

extend(elements)

Appends subelements from a sequence object with zero or more elements.

find(path, namespaces=None)

Finds first matching element by tag name or path.

findall(path, namespaces=None)

Finds all matching subelements by name or path.

findfirst(*paths)

Given a list of paths, returns the first matching element.

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

Finds text for first matching element by name or path.

get(key, default=None)

Gets the element attribute named key.

has(key)
insert(index, subelement)

Inserts subelement at the given position in this element.

items()

Returns the element attributes as a sequence of (name, value) pairs.

iter(tag=None)

Creates a tree iterator.

iterfind(path, namespaces=None)

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

itertext()

Creates a text iterator and returns all inner text.

keys()

Returns the elements attribute names as a list.

makeelement(tag, attrib)
remove(subelement)

Removes the other element from the element.

remove_namespaces()

Removes all namespace references from the element and its children.

set(key, value)

Set the attribute key on the element to value.

textdict(*paths, deep=False)

Collects texts from child elements.

Parameters:
  • paths – List of paths to search for.

  • deep – If False it only looks into direct children, otherwise search the entire subtree.

Returns:

A dict tag name -> text.

textlist(*paths, deep=False)

Collects texts from child elements.

Parameters:
  • paths – List of paths to search for.

  • deep – If False it only looks into direct children, otherwise search the entire subtree.

Returns:

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

textof(*paths)

Given a list of paths, returns the text of the first matching element that has text.

to_dict()

Creates a dictionary from an XmlElement object.

to_list(opts=None)

Parse an XML element into a list of arguments (reverse of gws.lib.xmlx.tag).

Parameters:

opts – XML options for serialization.

to_str(opts=None)
to_string(opts=None)

Converts the Element object to a string.

Parameters:

opts – XML options for serialization.

Returns:

An XML string.