我是用python编程的新手,我在理解这个概念时遇到了一些困难。我想比较两个xml文件。这些xml文件相当大。 我将给出一个我想要比较的文件类型的例子。
XML文件1:
<xml>
<property1>
<property2>
<property3>
</property3>
</property2>
</property1>
</xml>
xml文件2:
<xml>
<property1>
<property2>
<property3>
<property4>
</property4>
</property3>
</property2>
</property1>
</xml>
我命名的property1,property2与文件中实际的property2不同。xml文件中有很多属性。 我想比较一下这两个xml文件。
我正在使用一个lxml解析器来尝试比较这两个文件并打印出它们之间的差异。
我不知道如何分析和比较它自动。
我试图阅读lxml解析器,但我不知道如何使用它来解决我的问题。
有人能告诉我该如何处理这个问题吗。
代码片段非常有用
还有一个问题,我是在遵循正确的概念,还是我遗漏了其他东西?请纠正我你不知道的任何新概念
另一个使用xml.etree的脚本。很糟糕,但很有效:)
我解决这个问题的方法是将每个XML转换成一个xml.etree.ElementTree并遍历每个层。 我还提供了在进行比较时忽略属性列表的功能。
第一段代码包含使用的类:
第二段代码包含两个XML示例及其比较:
此代码的大部分功劳必须给予syawar
这实际上是一个相当具有挑战性的问题(由于“差异”在这里通常是指旁观者的眼睛,因为在语义上会有“等价”的信息,您可能不希望将其标记为差异)。
您可以尝试使用xmldiff,这是基于论文中的工作Change Detection in Hierarchically Structured Information。
相关问题 更多 >
编程相关推荐