如何比较XML文件 [英] How to compare XML files

查看:86
本文介绍了如何比较XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个由某些工具生成的XML文件(XSD)。

该工具不会保留元素的顺序,因此尽管内容相等,但将其作为文本进行比较会导致文件是不同的。

是否有一些工具可以在比较之前对元素进行排序,并可以对文档进行文本比较?
当然,排序需要递归进行。

I have two XML files (XSD) which are generated by some tool.
The tool doesn't preserve the order of elements so although the content is equal comparing it as text will result as the files are different.
Is there some tool that can sort the elements before comparing and will enable text comparison of the documents? Of course the sorting needs to be done recursively.

数据示例:

文件A:

Data example:
File A:

<xml>
  <A/>
  <B/>
</xml>

文件B:

<xml>
  <B/>
  <A/>
</xml>


推荐答案

我遇到了类似的问题,但最终发现:< a href = http://superuser.com/questions/79920/how-can-i-diff-two-xml-files> http://superuser.com/questions/79920/how-can-i-diff -two-xml-files

I had a similar problem and I eventually found: http://superuser.com/questions/79920/how-can-i-diff-two-xml-files

该帖子建议先进行规范的XML排序,然后再进行比较。如果您使用的是Linux,Mac或安装了Cygwin之类的Windows,则下面的命令应该适合您:

That post suggests doing a canonical XML sort then doing a diff. The following should work for you if you are on Linux, Mac, or if you have Windows with something like Cygwin installed:

$ xmllint --c14n FileA.xml > 1.xml
$ xmllint --c14n FileB.xml > 2.xml
$ diff 1.xml 2.xml

这篇关于如何比较XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆