COT.xml_file module¶
Reading, editing, and writing XML files.
- class XML[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 in ordering will result in COT logging a warning iff the unaccounted-for tag is in a known namespace.
- classmethod find_all_children(parent, tag, attrib={})[source]¶
Find all matching child elements under the specified parent element.
Parameters: - parent (xml.etree.ElementTree.Element) – Parent element
- tag (str) – Child tag 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={}, 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
- read_xml(xml_file)[source]¶
Read the given XML file and store it in memory.
The memory representation is available as self.tree and self.root.
Raises: - xml.etree.ElementTree.ParseError – if parsing fails under Python 2.7 or later
- xml.parsers.expat.ExpatError – if parsing fails under Python 2.6
Parameters: xml_file (str) – File path to read.
- 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“
- 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.