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.
- package com.example.Test;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.custommonkey.xmlunit.DetailedDiff;
- import org.custommonkey.xmlunit.Difference;
- import org.custommonkey.xmlunit.XMLUnit;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.Node;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- public class XmlCompare {
- public static void main(String[] args) throws Exception {
- String newFile = "F:\\CompareXml\\Res_New0.xml";
- String LegacyFile = "F:\\CompareXml\\Res_Legacy0.xml";
- compareXMLFiles(newFile, LegacyFile);
- //differenceinXMLFile(newFile, LegacyFile);
- }
- public static void compareXMLFiles(String newFile, String legacy)
- throws SAXException, IOException, ParserConfigurationException {
- boolean checkDifference=true;
- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
- Document doc1 = docBuilder.parse(newFile);
- Document doc2 = docBuilder.parse(legacy);
- NodeList n1 = doc1.getElementsByTagName("*");
- NodeList n2 = doc2.getElementsByTagName("*");
- List<String> nodesNewFile = new ArrayList<>();
- List<String> nodesLegacy = new ArrayList<>();
- for (int i = 6; i < n1.getLength(); i++) {
- nodesNewFile.add(n1.item(i).getNodeName().toString().trim());
- }
- for (int i = 6; i < n2.getLength(); i++) {
- nodesLegacy.add(n2.item(i).getNodeName().toString().trim());
- }
- Set<String> similarq = new HashSet<String>(nodesLegacy);
- Set<String> differentq = new HashSet<String>();
- differentq.addAll(nodesNewFile);
- differentq.addAll(nodesLegacy);
- differentq.removeAll(similarq);
- Collection<String> similar = new HashSet<String>(nodesNewFile);
- Collection<String> different = new HashSet<String>();
- different.addAll(nodesNewFile);
- different.addAll(nodesLegacy);
- //System.out.println("Diff");
- different.removeAll(similar);
- if (!differentq.isEmpty()) {
- System.out.println("***********FAIL*****************");
- checkDifference=false;
- String element = differentq.iterator().next();
- String[] el = element.split(":");
- NodeList baseElmntLst_gold = doc2.getElementsByTagName(n2.item(2).getNodeName());
- Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
- System.out.println("FileName:" + new File(newFile).getName());
- NamedNodeMap baseElmnt_gold_attr = baseElmnt_gold.getAttributes();
- for (int i = 0; i < baseElmnt_gold_attr.getLength(); ++i) {
- Node attr = baseElmnt_gold_attr.item(i);
- if (attr.getNodeName().contains(el[0])) {
- System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
- }
- }
- } else if (!different.isEmpty()) {
- System.out.println("***********FAIL*****************");
- checkDifference=false;
- String element = different.iterator().next();
- String[] el = element.split(":");
- NodeList baseElmntLst_gold = doc1.getElementsByTagName(n1.item(2).getNodeName());
- Element baseElmnt_gold = (Element) baseElmntLst_gold.item(0);
- System.out.println("FileName:" + new File(legacy).getName());
- NamedNodeMap baseElmnt_gold_attr = baseElmnt_gold.getAttributes();
- for (int i = 0; i < baseElmnt_gold_attr.getLength(); ++i) {
- Node attr = baseElmnt_gold_attr.item(i);
- if (attr.getNodeName().contains(el[0])) {
- System.out.println(attr.getNodeName() + " = \"" + attr.getNodeValue() + "\"");
- }
- }
- //System.out.println(different);
- }
- if(checkDifference){
- differenceinXMLFile(newFile,legacy);
- }
- }
- public static void differenceinXMLFile(String newFile, String legacy)
- throws SAXException, IOException, ParserConfigurationException {
- DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
- Document doc1 = docBuilder.parse(newFile);
- Document doc2 = docBuilder.parse(legacy);
- List<String> ls = new ArrayList<>();
- XMLUnit.setIgnoreWhitespace(true);
- XMLUnit.setIgnoreAttributeOrder(true);
- DetailedDiff diff = new DetailedDiff(XMLUnit.compareXML(doc1, doc2));
- List<Difference> allDifferences = diff.getAllDifferences();
- for (Difference diffVale : allDifferences) {
- ls.add("<" + diffVale.getControlNodeDetail().getNode().getParentNode().getNodeName() + ">"
- + diffVale.getControlNodeDetail().getValue() + "</"
- + diffVale.getControlNodeDetail().getNode().getParentNode().getNodeName() + ">");
- // System.out.println();
- // System.out.println();
- // System.out.println(diffVale.getTestNodeDetail());
- }
- boolean disp=true;
- if (!ls.isEmpty()) {
- for (int i = 5; i < ls.size(); i++) {
- System.out.println(ls.get(5));
- disp=false;
- }
- }
- if(disp){
- System.out.println("Success No Difference Anywhere");
- }
- // Assert.assertEquals("Differences found: "+ diff.toString(), 0,
- // allDifferences.size());
- }
- }
Comments
Post a Comment