我想比较 2 个 XML 文件的 XML 树结构:
文件 1:
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?><DATA>
<Time>2016-02-12T00:00:00.211Z</Time>
<Item>
<ID>123456789</ID>
<VAR1>897654564dDJUHFKHJHEU</VAR1>
</Item>
<Check>OK</Check>
</DATA>
文件 2:
<?xml version = '1.0' encoding = 'UTF-8' standalone = 'yes'?><DATA>
<Time>2016-02-13T00:05:00.531Z</Time>
<Item>
<ID>888555444</ID>
<VAR1>ABCD_MKLS8866dee6</VAR1>
</Item>
<Check>FAILED</Check>
</DATA>
我的代码:
from lxml import etree
tree1 = etree.parse('file1.xml')
tree2 = etree.parse('file2.xml')
print(tree1.getroot())
print(tree2.getroot())
print set(tree1.getroot()) == set(tree2.getroot())
当我比较它们时,结果 == 假。结果应该是 == 真...如何仅比较树结构而不与值进行比较?
为什么它们不等同是一个很好的问题。我得自己查一下。但是,如果使用.iter()
展平树,则可以比较列表等效性 - 为了清楚起见,以下示例很详细:
from lxml import etree
xml_1 = '<tag1><tag2></tag2></tag1>'
xml_2 = '<tag1><tag2></tag2></tag1>'
xml_1_parsed = etree.fromstring(xml_1)
xml_2_parsed = etree.fromstring(xml_2)
xml_1_flattened = [i.tag for i in xml_1_parsed.iter()]
xml_2_flattened = [i.tag for i in xml_2_parsed.iter()]
print xml_1_flattened == xml_2_flattened