Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from xml.dom.minidom import parse
- import sys
- def walk_nodes(root, type_filter=(), bad_names=('#text',)):
- """Return all nodes in the tree as a list"""
- traversed = [] #Traversed nodes
- to_traverse = [root] #Nodes we have not yet visited
- while len(to_traverse) > 0: #Depth first traversal
- current = to_traverse.pop()
- if (current.nodeType in type_filter or len(type_filter) == 0) and current.nodeName not in bad_names:
- to_traverse.extend(current.childNodes)
- #Compensate for weird #text nodes
- if current.nodeName not in bad_names:
- traversed.append(current)
- return traversed
- def find_nodes(root, node_name):
- """Finds all nodes with the given name under root"""
- return [node for node in walk_nodes(root) if node.nodeName == node_name]
- def compare_nodes(root1, root2, element_name='Signal', attr='Name'):
- """Given two root nodes, find the elements with the given name and do a set difference based on the attribute name"""
- attr1 = set([element.getAttribute(attr) for element in root1.getElementsByTagName(element_name)])
- attr2 = set([element.getAttribute(attr) for element in root2.getElementsByTagName(element_name)])
- return attr2 - attr1
- def choice_nodes(root):
- """Find all possible element names in the tree"""
- return set([node.nodeName for node in walk_nodes(root, (1,))])
- if __name__ == '__main__':
- assert len(sys.argv) >= 3, 'You must provide valid xml file!'
- #Move past the Document tag
- dom1 = parse(sys.argv[1]).firstChild
- dom2 = parse(sys.argv[2]).firstChild
- for name in compare_nodes(dom1, dom2):
- print name
Add Comment
Please, Sign In to add comment