import xml.etree.ElementTree def get_text_or_default(parent_node: xml.etree.ElementTree.Element, node_names, default_value): if not isinstance(node_names, list): node_names = [node_names] node = parent_node for n in node_names: node = node.find(n) if node is None: return default_value return node.text def parse_mos_xml(mosElem: xml.etree.ElementTree.Element) -> bool: mosId = mosElem.find("mosID").text mosObj = mosElem.find("mosObj") objId = mosObj.find("objID").text status = mosObj.find("status").text if status == "DELETED": objData = { "mosId": mosId, "objId": objId, "isDeleted": 1 } return objData objData = { "mosId": mosId, "objId": objId, "status": status, "isDeleted": 0, "objSlug": get_text_or_default(mosObj, "objSlug", ""), "objPath": get_text_or_default(mosObj, ["objPaths", "objPath"], ""), "objProxyPath": get_text_or_default(mosObj, ["objPaths", "objProxyPath"], ""), "objGroup": get_text_or_default(mosObj, "objGroup", ""), "objAir": get_text_or_default(mosObj, "objAir", ""), "created": get_text_or_default(mosObj, "created", ""), "createdBy": get_text_or_default(mosObj, "createdBy", ""), "changed": get_text_or_default(mosObj, "changed", ""), "changedBy": get_text_or_default(mosObj, "changedBy", ""), "description": get_text_or_default(mosObj, "description", ""), "objDur": get_text_or_default(mosObj, "objDur", "0"), "planningId": get_text_or_default(mosObj, ["mosExternalMetadata", "mosPayload", "planningID"], "") } #print(repr(objData)) return objData