问题
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:
A file:
<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 file:
<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>
- The thing which i want to ask is how to compare A file with B file making sure that though the order of "location" element is different in both the files, still they are shown same using python?
- I have tried all kinds of approach and also tried referring to this question, but in this project i want to develop an approach of my own and I cant use any already available tools.
The approach which I have tried so far is:
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?
Thank you.
回答1:
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.
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?
来源:https://stackoverflow.com/questions/31111496/comparing-two-xml-files-irrespective-of-their-order