I have some nearly identical XML that I am try to compare, and having found this: Compare XML snippets? which pointed to this: https://bitbucket.org/ianb/formencode/src/tip/formencode/doctest_xml_compare.py#cl-70 I have a way of testing two nodes.
The next step is to take the output from the node based test, and if False
, step into all the children, and repeat the test.
I have written a walker the long way, that allows me to step through as many children as I want to write the code for:
if xml.xml_compare(a.root, b.root) == False:
for i, node in enumerate(a.root):
if xml.xml_compare(a.root[i], b.root[i]) == False:
for j, node in enumerate(a.root[i]):
if xml.xml_compare(a.root[i][j], b.root[i][j]) == False:
for k, node in enumerate(a.root[i][j]):
....
if xml.xml_compare(a.root[i][j][k][l][m][n], b.root[i][j][k][l][m][n]) == False:
This is clearly not suitable for arbitrary sized XML, and its not very elegant. I thnk I need to write a generator to walk the XML under test - I saw that itertool is a way of doing this:
class XML_Tools(object):
....
def iterparent(self, xml_object):
"""
returns the parent and children of a node
"""
for parent in xml_object.getiterator():
for child in parent:
yield self.parent, self.child
main():
a = ET.parse(open(file_a, "r")
b = ET.parse(open(file_b, "r")
xml.iterparent(a.root)
for xml.parent, xml.child in xml.iterparent(a.root):
print xml.parent, xml.child
But I couldn't find a way of getting a working xml.parent or xml.child object that I can function on. I suspect I've messed up moving the function into a Class, and am not giving/getting the right things.
What I want to do is find the source of a False comparison, and print the two offending data elements, and know where they live (or are missing from) in the two pieces of XML.