__init__ – bridge init

class bridge.Attribute(name=None, value=None, prefix=None, namespace=None, parent=None)

Maps the attribute of an XML element to a simple Python object.

Note that names containing dot will see those replaced by underscores.

Parameters:
  • name: attribute’s name`(unicode)
  • value: content of the attribute (unicode)
  • prefix: XML prefix of the element (unicode)
  • namespace: XML namespace defining the prefix (unicode)
  • parent: element which this attribute belongs to.
class bridge.Element(name=None, content=None, attributes=None, prefix=None, namespace=None, parent=None)

Maps an XML element to a Python object.

If parent is not None, self will be added to the parent.xml_children list.

If Element.as_list is set and if (name, namespace) belongs to it then we will add a list to parent with the name of the element

If Element.as_attribute is set and if (name, namespace) belongs to it then we will add an attribute to parent with the name of the element

Parameters:
  • name – Name of the XML element (unicode)
  • content: Content of the element (unicode)
  • attributes: dictionary of the form {local_name: value}
  • prefix: XML prefix of the element (unicode)
  • namespace: XML namespace attached to that element (unicode)
  • parent: Parent element of this element.
clone()
Creates a new instance of the current element. The entire subtree is cloned as well.
collapse(separator='n')
Collapses all content of this element and its entire subtree.
filtrate(some_filter, **kwargs)

Applies a filter to this element. Returns what is returned from some_filter.

Parameters:
  • some_filter: a callable
  • kwargs: any additional data to pass to some_filter
forget()
Deletes this instance of Element. It will also removes it from its parent children and attributes.
get_child(name, ns=None)

Returns the child element named ‘name’, None if not found.

Parameters:-name: local name of the element - ns: namespace of the element
get_children(name, ns=None)

Yields all children of this element named ‘name’

Parameters:
  • name: local name of the element
  • ns: namespace of the element
get_children_with(types=None)

Returns a list of children belonging only to the types passed in types which must be a list or None. If None is passed then returns self.xml_children.

>>> feed.get_children_with(types=[Element])

This will return all the children which are of type bridge.Element

get_children_without(types=None)

Returns a list of children not belonging to the types passed in types which must be a list or None. If None is passed then returns self.xml_children.

>>> feed.get_children_without(types=[str, Comment])

This will return all the children which are not of type string or bridge.Comment.

has_child(name, ns=None)

Checks if this element has a child named ‘name’ in its children elements

Parameters:
  • name: local name of the element
  • ns: namespace of the element
insert_after(after_element, element)

Insert element right after after_element. This only inserts the new element in self.xml_children.

Parameters:
  • after_element: element pivot
  • element: new element to insert
insert_before(before_element, element)

Inserts element right before before_element. This only inserts the new element in self.xml_children.

Parameters:
  • before_element: element pivot
  • element: new element to insert
is_mixed_content()
Returns True if the direct children of this element makes are in mixed content.
classmethod load(source, prefixes=None)

Load source into an Element instance

Parameters:
  • source: an XML string, a file path or a file object
  • prefixes: dictionnary of prefixes of the form {‘prefix’: ‘ns’}
remove_from(element)
Removes the instance from the element parameter provided.
replace(current_element, new_element)

Replaces the current element with a new element in the list of children.

Parameters:
  • current_element: element pivot
  • new_element: new element to insert
set_attribute_value(qname, value)

Sets the attribute value. If the attribute does not exist it is created and set with name`and `value.

Returns the set attribute instance.

update_prefix(dst, srcns, dstns, update_attributes=True)

Updates prefixes of all the element of document matching (src, srcns)

Parameters:
  • dst: new prefix to be used
  • srcns: source namespace
  • dstns: destination namespace
  • update_attributes: update attributes’ namespace as well (default: True)
validate(validator, **kwargs)
Applies a validator on this element
xml(indent=True, encoding='UTF-8', prefixes=None, omit_declaration=False)

Serializes this element as a string.

Parameters:
  • indent: pretty print the XML string (defaut: True)
  • encoding: encoding to use during the serialization process
  • prefixes: dictionnary of prefixes of the form {‘prefix’: ‘ns’}
  • omit_declaration: prevent the result to start with the XML declaration
Returns:

The XML string representing the current element.

xml_root
Retrieve the top level element
class bridge.PI(target, data, parent=None)

Represents a XML processing instruction.

Parameters:
  • target: The PI target value
  • data: The PI value itself
  • parent: Parent to which attach this PI
class bridge.Comment(data, parent=None)

Represents a XML comment

Parameters:
  • data: The comment value
  • parent: Parent to which attach this comment

Previous topic

bridge Reference documentation

This Page