def get_object(file_name='Account.object'): parser = etree.XMLParser(remove_blank_text=True) x = etree.parse(file_name).getroot() primary_nodes = [] name = file_name.split(".", 1)[0] obj_type = file_name.split(".", 1)[1] meta = Metadata(name=file_name, obj_type=obj_type) for node in x.getchildren(): node_tag = node.tag.replace(NS_FULL,"") if node_tag not in primary_nodes: meta.add_child(node_tag) primary_nodes.append(node_tag) for node in primary_nodes: for child_node in x.xpath("sf:" + node, namespaces=NAMESPACES): if not child_node.getchildren(): meta.add_property(child_node.tag.replace(NS_FULL, ""), child_node.text) else: cm = Metadata(name=child_node.tag.replace(NS_FULL, "")) for pr in child_node.getchildren(): if not pr.getchildren(): cm.add_property(pr.tag.replace(NS_FULL, ""), pr.text) else: name, sub_meta = get_child_node(pr) cm.add_child(pr.tag.replace(NS_FULL, ""), value=sub_meta) (meta.__dict__[node]).append(cm) return meta
def get_child_node(node): sub_meta = [] name = "" for pr in node.getchildren(): name = pr.tag.replace(NS_FULL, "") sm = Metadata() has_children = False for check_children in pr.getchildren(): if check_children.getchildren(): has_children = True break if pr.getchildren() and has_children: r_name, r_meta = get_child_node(pr) sm.add_child(name, value=r_meta) elif pr.getchildren and not has_children: for sv in pr.getchildren(): sm.add_property(sv.tag.replace(NS_FULL, ""), sv.text) else: sm.add_property(sv.tag.replace(NS_FULL, ""), sv.text) sub_meta.append(sm) return name, sub_meta