Compare two xml and find missing node using Java

It bothered me for several days. I just need to find out which missing nodes in the second xml are compared. I tried xmlunit, but it really didn’t help me find the missing node, as it compares line by line. two xml samples: xml1:

<test testcase="101"> <value="1"> <value="2"> <value="3"> </test> 

xml2:

 <test testcase="101"> <value="3"> <value="2"> </test> 

Note that value = 3 is actually there, in the second copy, it just goes to the first node, but still I believe that it is not lost. how to find the missing node value = "1"?

+5
source share
1 answer

I need to try it work

 package WebInitalizar; import java.util.List; import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.XMLUnit; import org.junit.Assert; public class TestXml { public static void main(String[] args) throws Exception { String result = "<test testcase=\"101\"><value1>10</value1></test>"; // will be ok assertXMLEquals("<test testcase=\"1010\"><value1>10</value1><value2>10</value2></test>", result); } public static void assertXMLEquals(String expectedXML, String actualXML) throws Exception { XMLUnit.setIgnoreWhitespace(true); XMLUnit.setIgnoreAttributeOrder(true); DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(expectedXML, actualXML)); List<?> allDifferences = diff.getAllDifferences(); System.out.println(allDifferences); Assert.assertEquals("Differences found: "+ diff.toString(), 0, allDifferences.size()); } } 
0
source

All Articles