如何在XMLUnit中比较两个相似的XML文件

如何在XMLUnit中比较两个相似的XML文件

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

问题描述

我想使用 XMLUnit 比较两个相似的XML文件.

I want to use XMLUnit to compare two similar XML files.

基本上每件事都是一样的,File1File2的副本,但是在File2中,我更改了一个节点中某些元素的顺序.

Basically every thing is same, File1 is a copy of File2 , but in File2 I have changed the order of some elements in one node.

我正在尝试运行一个测试,比较这些文件并返回类似的结果,而不将这些文件视为不同的结果.

I am trying to run a test where it compares these files and returns a result of similar and not treat these files as different.

推荐答案

我认为此链接可以为您提供帮助- http://www.ibm.com/developerworks/java/library/j-cq121906.html#N10158

I think this link can help you - http://www.ibm.com/developerworks/java/library/j-cq121906.html#N10158

基本上,如果您的File1像--

Basically, if your File1 is like -

<account>
 <id>3A-00</id>
 <name>acme</name>
</account>

和File2相同,但仅在<name><id>的顺序上有所不同-

And File2 is same, but only differs in order of <name> and <id> -

<account>
 <name>acme</name>
 <id>3A-00</id>
</account>

然后,您可以编写如下所示的测试,将其进行比较并返回类似结果.

Then you can write a test like below which will compare these and return similar.

public void testIdenticalAndSimilar() throws Exception {
   String controlXML = "<account><id>3A-00</id><name>acme</name></account>";
   String testXML = "<account><name>acme</name><id>3A-00</id></account>";
   Diff diff = new Diff(controlXML, testXML);
   assertTrue(diff.similar());
   assertFalse(diff.identical());
}

希望有帮助.

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

08-14 11:15