【发布时间】:2020-11-05 15:12:12
【问题描述】:
我想使用 Python 比较 2 个 xml 并获得特定的输出。
示例:
old.xml
<foos>
<foo>
<id>1</id>
<x>1</x>
</foo>
<foo>
<id>2</id>
<x>1</x>
</foo>
<foo>
<id>3</id>
<x>1</x>
<y>1</y>
</foo>
</foo>
new.xml
<foos>
<foo>
<id>1</id>
<x>2</x>
<y>1</y>
</foo>
<foo>
<id>2</id>
<x>1</x>
</foo>
<foo>
<id>3</id>
<x>2</x>
<y>1</y>
</foo>
<foo>
<id>4</id>
<x>1</x>
</foo>
</foo>
还有我想要的输出:
output.xml
<foos>
<foo>
<id>1</id>
<x>2</x>
<y>1</y>
</foo>
<foo>
<id>3</id>
<x>2</x>
</foo>
<foo>
<id>4</id>
<x>1</x>
</foo>
</foo>
我写了一个性能很差的非常丑陋的函数,我想找到一个更好的方法来做到这一点。您对如何以良好的表现执行此任务有任何想法吗?
我遇到的一些问题;
- 2个xml的ids列表不相等(2个xml之间可以删除或添加对象)
- 输出的特定格式,阻止我使用经典库来完成这项工作 (https://github.com/Shoobx/xmldiff)。但也许有解决方法?
【问题讨论】:
标签: python xml performance compare xmldiff