1

I have two xmls with similar structure:

<root>
  <node>some_name0
    <value>some_int</value>
  </node>
  <node>some_name1
    <value>some_float</value>
  </node>
  <node>some_name2
    <value>some_sting</value>
 </node>
 <node>some_name3
    <value>some_bool</value>
 </node>
</root>

and second one:

<root>
  <node1>some_name0
    <value>some_int</value>
  </node1>
  <node1>some_name1
    <value>some_float</value>
  </node1>
  <node1>some_name2
    <value>some_sting</value>
 </node1>
 <node1>some_name3
    <value>some_bool</value>
 </node1>
</root>

How can I compare nodes with same names? The numbers of nodes in xml1 and xml2 can differ, so I cannot use the zip built-in.

from xml.etree import ElementTree as et
tr1 = et.parse(path1)
root1 = tr1.getroot()
tr2 = et.parse(path2)
root2 = tr2.getroot()
for child1 in root1.getchildren():
    for child2 in root2.getchildren():
        if child1.text==child2.text:
           for ch1, ch2 in zip(child1.getchildren(), child2.getchildren())
              if ch1.tag !=ch2.tag:
                 print "node {0} are differ from same node values {1} and {2} not equal".format(child1, ch1, ch2)
           break
        elif child2 == root2.getchildren()[-1]:
           print "Missed element {0} in second xml!".format(child2.text)

How can I do it prettier and shorter? Right now process can take a long time.

martineau
  • 119,623
  • 25
  • 170
  • 301
nick_gabpe
  • 5,113
  • 5
  • 29
  • 38
  • I saw but it does not worked for me, i want use only standard libraries. – nick_gabpe Oct 21 '16 at 13:18
  • You don't want to use only standard libraries. If someone wrote a library that solves the problem of diffing/comparing XML then you want to use that library, instead of rolling your own. – Tomalak Oct 21 '16 at 13:37

0 Answers0