http://www.xmlunit.org/
org.xmlunit xmlunit-core 2.3.0
123 liming
1234 liming
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) { Iterabledifferences = build.getDifferences(); for (Difference difference : differences) { System.out.println(difference); } } assertFalse(hasDifferences); }}
运行结果:
Expected text value '123' but was '1234' - comparing123 at /root[1]/id[1]/text()[1] to1234 at /root[1]/id[1]/text()[1] (DIFFERENT)