http://www.xmlunit.org/
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.3.0</version>
</dependency>
<root>
<id>123</id>
<name>liming</name>
</root>
control.xml
<root>
<id>1234</id>
<name>liming</name>
</root>
test.xml
import static org.junit.Assert.assertFalse; import java.io.File; import javax.xml.transform.Source; import org.junit.Test;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.Diff;
import org.xmlunit.diff.Difference; public class Xmltest { @Test
public void ff() {
File control = new File("E://control.xml");
File test = new File("E://test.xml"); Source sourceControl = Input.fromFile(control).build();
Source sourceTest = Input.fromFile(test).build(); Diff build = DiffBuilder.compare(sourceControl).withTest(sourceTest).ignoreComments().ignoreWhitespace().build(); boolean hasDifferences = build.hasDifferences();
if (hasDifferences) {
Iterable<Difference> differences = build.getDifferences();
for (Difference difference : differences) {
System.out.println(difference);
}
}
assertFalse(hasDifferences);
} }
运行结果:
Expected text value '123' but was '1234' - comparing <id ...>123</id> at /root[1]/id[1]/text()[1] to <id ...>1234</id> at /root[1]/id[1]/text()[1] (DIFFERENT)