COT.xml_file module¶
Reading, editing, and writing XML files.
-
class
XML(xml_file)[source]¶ Bases:
objectClass capable of reading, editing, and writing XML files.
-
__init__(xml_file)[source]¶ Read the given XML file and store it in memory.
The memory representation is available as properties
treeandroot.Parameters: xml_file (str) -- File path to read. Raises: xml.etree.ElementTree.ParseError-- if parsing fails
-
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
orderingis given, any tag (new or existing) that is encountered but not accounted for inorderingwill result in COT logging a warning if and only if 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 (iterable) -- Child tag string (or list of tags) to match on
- attrib (dict) -- Child attributes to match on
Returns: list -- (Possibly empty) list of matching child Elements
-
classmethod
find_child(parent, tag, attrib=None, required=False)[source]¶ Find the unique child element 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
- required (boolean) -- Whether to raise an error if no child exists
Raises: LookupError-- if more than one matching child is foundKeyError-- if no matching child is found andrequiredis True
Returns: xml.etree.ElementTree.Element -- Child element found, or None
-
static
get_ns(text)[source]¶ Get the namespace prefix from an XML element or attribute name.
Parameters: text (str) -- Element name or attribute name, such as "{http://schemas.dmtf.org/ovf/envelope/1}Element". Returns: str -- "" if no prefix is present, or a namespace prefix, 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: xml.etree.ElementTree.Element -- New or updated child Element.
-
static
strip_ns(text)[source]¶ Remove a namespace prefix from an XML element or attribute name.
Parameters: text (str) -- Element name or attribute name, such as "{http://schemas.dmtf.org/ovf/envelope/1}Element". Returns: str -- Bare name, such as "Element".
-
write_xml(xml_file)[source]¶ Write pretty XML out to the given file.
Parameters: xml_file (str) -- Filename to write to
-
static
xml_reindent(parent, depth=0)[source]¶ Recursively add indentation to XML to make it look nice.
Parameters: - parent (xml.etree.ElementTree.Element) -- Current parent element
- depth (int) -- How far down the rabbit hole we have recursed. Increments by 2 for each successive level of nesting.
-
root= None¶ Root
xml.etree.ElementTree.Elementinstance of the tree.
-
tree= None¶ xml.etree.ElementTree.ElementTreedescribing this file.
-