我目前正在做一个python项目,遇到了一个与使用python比较两个XML文件有关的小问题。例如,假设我们有两个xml文件:
文件:
<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文件:
^{pr2}$到目前为止,我尝试的方法是:
我正在使用LXML,从一个文件中获取子元素的各个属性,并将它们存储在列表中。然后我将比较B文件的元素和子属性与该列表中存储的值。在
首先,这种方法行不通,我也无法想出任何有效的程序来完成这项任务。你们能解释一下吗?在
谢谢。在
听起来你需要一些XML解析器。 我的第一个建议是使用DOM解析器(或者自己创建一个非常基本的解析器)。通过读取这两个XML文件,然后比较这些树,可以很容易地验证它们是否相同。在
但这不是很有效。可以在读取第二个XML文件时进行验证。但是,您必须删除匹配的元素。(确保没有留下不匹配的元素)
但我很好奇为什么你的列表方法不起作用。你能提供更多的信息吗?在
相关问题 更多 >
编程相关推荐