title: How to compare and get difference between two xml files using java?
body: <p>I have two xml files as below.</p>
  
  <p><em>Compare.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;class&gt;
  &lt;student rollno="393"&gt;
    &lt;firstname&gt;dinkar&lt;/firstname&gt;
    &lt;lastname&gt;kad&lt;/lastname&gt;
    &lt;nickname&gt;dinkar&lt;/nickname&gt;
    &lt;marks&gt;85&lt;/marks&gt;
  &lt;/student&gt;
  &lt;student rollno="493"&gt;
    &lt;firstname&gt;Vaneet&lt;/firstname&gt;
    &lt;lastname&gt;Gupta&lt;/lastname&gt;
    &lt;nickname&gt;vinni&lt;/nickname&gt;
    &lt;marks&gt;95&lt;/marks&gt;
  &lt;/student&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p><em>Reference.xml</em></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;class&gt;
  &lt;student rollno="393"&gt;
    &lt;firstname&gt;ila&lt;/firstname&gt;
    &lt;lastname&gt;kad&lt;/lastname&gt;
    &lt;nickname&gt;dinkar&lt;/nickname&gt;
    &lt;marks&gt;85&lt;/marks&gt;
  &lt;/student&gt;
  &lt;student rollno="493"&gt;
    &lt;firstname&gt;Vaneet&lt;/firstname&gt;
    &lt;lastname&gt;Gupta&lt;/lastname&gt;
    &lt;nickname&gt;vinni&lt;/nickname&gt;
    &lt;marks&gt;95&lt;/marks&gt;
  &lt;/student&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>Now I need to compare and get the difference between these two xml     files. I also need the output to be exported as a log file.</p>
  
  <p>Below my code:</p>
  
  <pre><code>package DomParserDemo;
  import java.io.File;
  import java.util.logging.FileHandler;
  import java.util.logging.Logger;
  
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.DocumentBuilder;
  
  import org.w3c.dom.Document;
  import org.w3c.dom.NodeList;
  import org.w3c.dom.Node;
  import org.w3c.dom.Element;
  
  public class DomParserDemo {
  public static void main(String[] args){
  
    try { 
       File inputFile = new File("input.txt");
       DocumentBuilderFactory dbFactory 
          = DocumentBuilderFactory.newInstance();
       DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
       Document doc = dBuilder.parse(inputFile);
       doc.getDocumentElement().normalize();
       System.out.println("Root element :" 
          + doc.getDocumentElement().getNodeName());
       NodeList nList = doc.getElementsByTagName("student");
       System.out.println("----------------------------");
       for (int temp = 0; temp &lt; nList.getLength(); temp++) {
          Node nNode = nList.item(temp);
          System.out.println("\nCurrent Element :" 
             + nNode.getNodeName());
          if (nNode.getNodeType() == Node.ELEMENT_NODE) {
             Element eElement = (Element) nNode;
             System.out.println("Student roll no : " 
                + eElement.getAttribute("rollno"));
             System.out.println("First Name : " 
                + eElement
                .getElementsByTagName("firstname")
                .item(0)
                .getTextContent());
             System.out.println("Last Name : " 
             + eElement
                .getElementsByTagName("lastname")
                .item(0)
                .getTextContent());
             System.out.println("Nick Name : " 
             + eElement
                .getElementsByTagName("nickname")
                .item(0)
                .getTextContent());
             System.out.println("Marks : " 
             + eElement
                .getElementsByTagName("marks")
                .item(0)
                .getTextContent());
          }
       }
  
       FileHandler handler = new FileHandler("logfile.log");
  
       // Add to the desired logger
       Logger logger = Logger.getLogger("");
       logger.addHandler(handler);
       System.out.println("Log Generated Successfully...!!!");
  
  
    } catch (Exception e) {
        System.out.println("Log Generation Failed..!!!");
       e.printStackTrace();
    }
  }
  
  }
  </code></pre>
  
  <p>Now I can see the xml files in output with out tags and i need its difference and the output should be exported as a log file. </p>
  
  <p>Kindly help me to finish this and thanks in advance.</p>