比较两个XML文件,而不考虑它们的顺序 [英] comparing two xml files irrespective of their order
问题描述
我目前正在一个python项目,并停留在一个小问题与使用python比较两个XML文件。现在假设我们有两个xml文件:
I am currently working on a python project and stuck in one little problem related to comparison of two XML files using python. Now for instance assume that we have two xml files:
一个文件:
<m1:time timeinterval="5">
<m1:vehicle distance="40" speed="5"\>
<m1:location hours = "1" path = '1'\>
<m1:feature color="2" type="a">564</m1:feature>
<m1:feature color="3" type="b">570</m1:feature>
<m1:feature color="4" type="c">570</m1:feature>
<\m1:location>
<m1:location hours = "5" path = '1'\>
<m1:feature color="6" type="a">560</m1:feature>
<m1:feature color="7" type="b">570</m1:feature>
<m1:feature color="8" type="c">580</m1:feature>
<\m1:location>
<m1:location hours = "9" path = '1'\>
<m1:feature color="10" type="a">560</m1:feature>
<m1:feature color="11" type="b">570</m1:feature>
<m1:feature color="12" type="c">580</m1:feature>
<\m1:location>
</m1:time>
B档案:
<m1:time timeinterval="6">
<m1:vehicle distance="40" speed="5"\>
<m1:location hours = "5" path = '1'\>
<m1:feature color="6" type="a">560</m1:feature>
<m1:feature color="7" type="b">570</m1:feature>
<m1:feature color="8" type="c">580</m1:feature>
<\m1:location>
<m1:location hours = "1" path = '1'\>
<m1:feature color="2" type="a">564</m1:feature>
<m1:feature color="3" type="b">570</m1:feature>
<m1:feature color="4" type="c">570</m1:feature>
<\m1:location>
<m1:location hours = "9" path = '1'\>
<m1:feature color="10" type="a">560</m1:feature>
<m1:feature color="11" type="b">570</m1:feature>
<m1:feature color="12" type="c">580</m1:feature>
<\m1:location>
</m1:time>
- 我想要的是如何比较A文件B文件
确保虽然位置元素的顺序是不同的
在这两个文件,仍然显示相同使用python? - 我尝试过各种方法,并尝试引用
方法,我不能使用任何已经可用的工具。
我到目前为止所尝试的方法是:
The approach which I have tried so far is:
我正在使用LXML,子文件的个别属性从A文件并将其存储在列表中。然后我比较B文件的元素和子属性与存储在该列表中的值。
I am working with LXML and I am getting the individual attributes of children from A file and storing them in list. then I am comparing B file's elements and children attributes with the values stored in that list.
首先,这种方法不工作,我也不能想到任何有效的过程来完成这项任务。你能帮我们解释一下吗?
First all, this approach is not working and neither I am able to think of any efficient procedure to accomplish this task. Can you guys shed some light over this?
谢谢。
推荐答案
听起来像你需要一些XML解析器。
我的第一个建议是使用一个DOM解析器(或自己创建一个非常基本的)。通过读两个XML文件,然后比较树,你可以很容易地验证他们是否一样。
Sounds like you need some XML parser. My first suggestion would be to use a DOM parser (or create a very basic one yourself). By reading both XML files and then comparing the trees you can easily verify if they are the same.
这不是很有效率。可以在读取第二个XML文件时进行验证。然后,您将删除匹配的元素。 (为了确保没有不匹配的元素留下)
This is not very efficient though. It is possible to do the verification when reading the second XML file. You would then however have to remove the elements that match. (To make sure that no unmatched elements are left behind)
但我很好奇为什么你的列表方法不工作。你能提供一些更多的信息吗?
But I am curious why your list approach isn't working. Can you give some more information about this?
这篇关于比较两个XML文件,而不考虑它们的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!