Compare xml strings and list out difference in Python

76 views Asked by At

I've been looking around, and though there are several options, none of them really fit my current problem.

I get two XML strings, str1 and str2 that have "User" nodes with childnodes detailing things like name, age, address, etc. From there I need to create a third XML File3 that exclusively contains the fields that have changed between the two while keeping the "name" node to identify that the change was done with that user.

str1:
<users>
 <user>
  <name>Marco</name>
  <height>1,76</height>
  <address>C:/ Far-away 34</address>
  <mail>marco@marco</mail>
 </user>
</users>

str2:
<users>
 <user>
  <name>Marco</name>
  <height>1,80</height>
  <address>C:/ Far-away 34</address>
  <mail></mail>
 </user>
</users>

It should spit out an XML with:

str3:

<users>
 <user>
  <name>Marco</name>
  <height>1,80</height>
  <mail></mail>
 </user>
</users>

With one of my main complications being that I am not told which nodes will be present (or whether or not they have childnodes) other than "name". May I know, how can we do that Python without installing any pip package?.

1

There are 1 answers

0
HKTE On

You can use ElementTree for this. Parse both XML strings into ElementTree objects, use iter() to iterate over the user elements in both, and compare the text of each element:

import xml.etree.ElementTree as ET

tree1 = ET.fromstring(str1)
tree2 = ET.fromstring(str2)

result = ET.Element('users')

for user1, user2 in zip(tree1.iter('user'), tree2.iter('user')):
    changed = ET.Element('user')
    changed.append(user1.find('name'))
    for elem1, elem2 in zip(user1, user2):
        if elem1.text != elem2.text:
            changed.append(elem2)
    if len(changed) > 1:
        result.append(changed)