import xml.etree.ElementTree as ET
def find_diff(elem1, elem2, path=""):
"""递归查找两个Element对象的差异"""
diff = []
# 比较元素的标签
if elem1.tag != elem2.tag:
diff.append(f"{path}: tag differs - {elem1.tag} != {elem2.tag}")
# 比较元素的属性
for attr in set(elem1.attrib) | set(elem2.attrib):
if elem1.attrib.get(attr) != elem2.attrib.get(attr):
diff.append(f"{path}@{attr}: attribute differs - {elem1.attrib.get(attr)} != {elem2.attrib.get(attr)}")
# 比较元素的文本内容
if elem1.text and elem2.text and elem1.text.strip() != elem2.text.strip():
diff.append(f"{path}: text differs - {elem1.text.strip()} != {elem2.text.strip()}")
# 递归比较子元素
for child1, child2 in zip(elem1, elem2):
diff.extend(find_diff(child1, child2, path + "/" + elem1.tag))
# 检查是否有额外的子元素
if len(elem1) > len(elem2):
for extra in elem1[len(elem2):]:
diff.append(f"{path}/{elem1.tag}: extra element - {extra.tag}")
elif len(elem2) > len(elem1):
for extra in elem2[len(elem1):]:
diff.append(f"{path}/{elem2.tag}: extra element - {extra.tag}")
return diff
def compare_xml_files(file1, file2):
"""比较两个XML文件并打印差异"""
tree1 = ET.parse(file1)
tree2 = ET.parse(file2)
root1 = tree1.getroot()
root2 = tree2.getroot()
# 查找并打印差异
diff = find_diff(root1, root2)
for d in diff:
print(d)
# 使用示例
file1 = 'file1.xml'
file2 = 'file2.xml'
compare_xml_files(file1, file2)