简体   繁体   English

如何检查XML文件中的更改?

[英]How to check for changes in a XML file?

I want to make sure 2 XML files are the same, and the 2 options I see are. 我要确保2个XML文件相同,并且看到的2个选项是相同的。 1. Get the XML to have a last modification date with time, and check that is the same. 1.获取XML以具有最后修改日期和时间,并检查是否相同。 Problem is that someone may forget to change it and then it is useless. 问题是有人可能忘记更改它,然后变得毫无用处。 2. Create a hashcode of the xml file/structure. 2.创建xml文件/结构的哈希码。

I use a build in library in java, but the hash implementation can change from run to run which is not helpful 我使用Java中的内置库,但是哈希实现可以在运行之间变化,这无济于事

File model = new File(fileLocation);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(model);
doc.getHash();

is there an easy way to do this, and hopefully without importing another library. 有没有一种简单的方法来执行此操作,并且希望不导入其他库。

Will sorting the file and comparing help? 将文件排序和比较有帮助吗?

In case of libraries, You can try diffxml . 如果是库,可以尝试diffxml

XMLUnit has a diff feature XMLUnit具有差异功能

As Martin Thurau pointed out, it depends, whether you want to check if the files are identical, or if the XML content is identical. 正如Martin Thurau指出的,这取决于您是否要检查文件是否相同,或者XML内容是否相同。 This answer is only for the first case, when you want to know if two files are identical, i think that was your question because you suggested a hash value. 这个答案仅针对第一种情况,当您想知道两个文件是否相同时,我认为这是您的问题,因为您建议使用哈希值。

In this case building a hash is not a good choice, you can solve it easier like this: 在这种情况下,构建哈希不是一个好选择,您可以像这样更轻松地解决它:

  1. Check whether the file sizes are the same, if not they are not identical. 检查文件大小是否相同(如果不同)。
  2. Open a stream of both files. 打开两个文件的流。
  3. Just loop through the streams and stop by the first difference. 只需在溪流中循环并按第一个差异停下来即可。
  4. If no differences where found, they are identical. 如果没有发现差异,则它们是相同的。

So why not calculating a hash value? 那么,为什么不计算哈希值呢? You have to read the whole file anyway to calculate a hash, so when you compare the streams you can forgo the calculation, and you can stop by the first difference. 无论如何,您都必须阅读整个文件才能计算出哈希值,因此当您比较流时,就可以放弃计算,并且可以按第一个差值停下来。 Parsing the file to a DOM model will also have the same disadvantages. 将文件解析为DOM模型也将具有相同的缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM