COT.xml_file
module¶
Reading, editing, and writing XML files.
-
class
XML
(xml_file)[source]¶ Bases:
object
Class capable of reading, editing, and writing XML files.
-
classmethod
add_child
(parent, new_child, ordering=None, known_namespaces=None)[source]¶ Add the given child element under the given parent element.
Parameters: - parent (xml.etree.ElementTree.Element) – Parent element
- new_child (xml.etree.ElementTree.Element) – Child element to attach
- ordering (list) – (Optional) List describing the expected ordering of child tags under the parent; if a new child element is created, its placement under the parent will respect this sequence.
- known_namespaces (list) – (Optional) List of well-understood XML
namespaces. If a new child is created, and
ordering
is given, any tag (new or existing) that is encountered but not accounted for inordering
will result in COT logging a warning iff the unaccounted-for tag is in a known namespace.
-
classmethod
find_all_children
(parent, tag, attrib=None)[source]¶ Find all matching child elements under the specified parent element.
Parameters: - parent (xml.etree.ElementTree.Element) – Parent element
- tag (string or iterable) – Child tag (or list of tags) to match on
- attrib (dict) – Child attributes to match on
Return type: list of xml.etree.ElementTree.Element instances
-
classmethod
find_child
(parent, tag, attrib=None, required=False)[source]¶ Find the unique child element under the specified parent element.
Raises: - LookupError – if more than one matching child is found
- KeyError – if no matching child is found and
required
is True
Parameters: - parent (xml.etree.ElementTree.Element) – Parent element
- tag (str) – Child tag to match on
- attrib (dict) – Child attributes to match on
- required (boolean) – Whether to raise an error if no child exists
Return type: xml.etree.ElementTree.Element
-
classmethod
set_or_make_child
(parent, tag, text=None, attrib=None, ordering=None, known_namespaces=None)[source]¶ Update or create a child element under the specified parent element.
Parameters: - parent (xml.etree.ElementTree.Element) – Parent element
- tag (str) – Child element text tag to find or create
- text (str) – Value to set the child’s text attribute to
- attrib (dict) – Dict of child attributes to match on while searching and set in the final child element
- ordering (list) – See
add_child()
- known_namespaces (list) – See
add_child()
Returns: New or updated child Element.
Return type: xml.etree.ElementTree.Element
-
classmethod
strip_ns
(text)[source]¶ Remove a namespace prefix from an XML element or attribute name.
-
classmethod
-
register_namespace
(prefix, uri)[source]¶ Record a particular mapping between a namespace prefix and URI.
Parameters: - prefix (str) – Namespace prefix such as “ovf”
- uri (str) – Namespace URI such as “http://schemas.dmtf.org/ovf/envelope/1“