Compare XML using Java

It is developed specially for the SOAP service xml request and response where you can compare according to names and also you can Find the Difference in values.Refer Below code,


  • Compare two xml files.
  • By Using Difference method you can Get the Difference in values.
  • It Also Comapres XML Bi-Directional. 



  1. package com.example.Test;

  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.HashSet;
  7. import java.util.List;
  8. import java.util.Set;

  9. import javax.xml.parsers.DocumentBuilder;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import javax.xml.parsers.ParserConfigurationException;

  12. import org.custommonkey.xmlunit.DetailedDiff;
  13. import org.custommonkey.xmlunit.Difference;
  14. import org.custommonkey.xmlunit.XMLUnit;
  15. import org.w3c.dom.Document;
  16. import org.w3c.dom.Element;
  17. import org.w3c.dom.NamedNodeMap;
  18. import org.w3c.dom.Node;
  19. import org.w3c.dom.NodeList;
  20. import org.xml.sax.SAXException;

  21. public class XmlCompare {

  22. public static void main(String[] args) throws Exception {
  23. String newFile = "F:\\CompareXml\\Res_New0.xml";
  24. String LegacyFile = "F:\\CompareXml\\Res_Legacy0.xml";

  25. compareXMLFiles(newFile, LegacyFile);
  26. //differenceinXMLFile(newFile, LegacyFile);

  27. }

  28. public static void compareXMLFiles(String newFile, String legacy)
  29. throws SAXException, IOException, ParserConfigurationException {

  30. boolean checkDifference=true;
  31. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  32. DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  33. Document doc1 = docBuilder.parse(newFile);
  34. Document doc2 = docBuilder.parse(legacy);

  35. NodeList n1 = doc1.getElementsByTagName("*");
  36. NodeList n2 = doc2.getElementsByTagName("*");

  37. List<String> nodesNewFile = new ArrayList<>();
  38. List<String> nodesLegacy = new ArrayList<>();

  39. for (int i = 6; i < n1.getLength(); i++) {
  40. nodesNewFile.add(n1.item(i).getNodeName().toString().trim());

  41. }

  42. for (int i = 6; i < n2.getLength(); i++) {
  43. nodesLegacy.add(n2.item(i).getNodeName().toString().trim());

  44. }
  45. Set<String> similarq = new HashSet<String>(nodesLegacy);
  46. Set<String> differentq = new HashSet<String>();
  47. differentq.addAll(nodesNewFile);
  48. differentq.addAll(nodesLegacy);
  49. differentq.removeAll(similarq);

  50. Collection<String> similar = new HashSet<String>(nodesNewFile);
  51. Collection<String> different = new HashSet<String>();
  52. different.addAll(nodesNewFile);
  53. different.addAll(nodesLegacy);
  54. //System.out.println("Diff");
  55. different.removeAll(similar);

  56. if (!differentq.isEmpty()) {
  57. System.out.println("***********FAIL*****************");
  58. checkDifference=false;
  59. String element = differentq.iterator().next();
  60. String[] el = element.split(":");
  61. NodeList baseElmntLst_gold = doc2.getElementsByTagName(n2.item(2).getNodeName());
  62. Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
  63. System.out.println("FileName:" + new File(newFile).getName());
  64. NamedNodeMap baseElmnt_gold_attr = baseElmnt_gold.getAttributes();
  65. for (int i = 0; i < baseElmnt_gold_attr.getLength(); ++i) {

  66. Node attr = baseElmnt_gold_attr.item(i);
  67. if (attr.getNodeName().contains(el[0])) {
  68. System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
  69. }
  70. }

  71. } else if (!different.isEmpty()) {
  72. System.out.println("***********FAIL*****************");
  73. checkDifference=false;
  74. String element = different.iterator().next();
  75. String[] el = element.split(":");
  76. NodeList baseElmntLst_gold = doc1.getElementsByTagName(n1.item(2).getNodeName());
  77. Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
  78. System.out.println("FileName:" + new File(legacy).getName());
  79. NamedNodeMap baseElmnt_gold_attr = baseElmnt_gold.getAttributes();
  80. for (int i = 0; i < baseElmnt_gold_attr.getLength(); ++i) {

  81. Node attr = baseElmnt_gold_attr.item(i);
  82. if (attr.getNodeName().contains(el[0])) {
  83. System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
  84. }
  85. }

  86. //System.out.println(different);

  87. }

  88. if(checkDifference){
  89. differenceinXMLFile(newFile,legacy);

  90. }

  91. public static void differenceinXMLFile(String newFile, String legacy)
  92. throws SAXException, IOException, ParserConfigurationException {

  93. DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
  94. DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
  95. Document doc1 = docBuilder.parse(newFile);
  96. Document doc2 = docBuilder.parse(legacy);
  97. List<String> ls = new ArrayList<>();

  98. XMLUnit.setIgnoreWhitespace(true);
  99. XMLUnit.setIgnoreAttributeOrder(true);

  100. DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(doc1, doc2));

  101. List<Difference> allDifferences = diff.getAllDifferences();

  102. for (Difference diffVale : allDifferences) {

  103. ls.add("<" + diffVale.getControlNodeDetail().getNode().getParentNode().getNodeName() + ">"
  104. + diffVale.getControlNodeDetail().getValue() + "</"
  105. + diffVale.getControlNodeDetail().getNode().getParentNode().getNodeName() + ">");
  106. // System.out.println();
  107. // System.out.println();
  108. // System.out.println(diffVale.getTestNodeDetail());
  109. }
  110. boolean disp=true;
  111. if (!ls.isEmpty()) {
  112. for (int i = 5; i < ls.size(); i++) {
  113. System.out.println(ls.get(5));
  114. disp=false;
  115. }
  116. }
  117. if(disp){
  118. System.out.println("Success No Difference Anywhere");
  119. }

  120. // Assert.assertEquals("Differences found: "+ diff.toString(), 0,
  121. // allDifferences.size());
  122. }

  123. }




Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation