Java XML Node Print printDifferences(Node node1, Node node2)

Here you can find the source of printDifferences(Node node1, Node node2)

Description

print Differences

License

Open Source License

Declaration

public static void printDifferences(Node node1, Node node2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static void printDifferences(Node node1, Node node2) {
        if (node1.getNodeType() == Node.DOCUMENT_NODE)
            node1 = ((Document) node1).getDocumentElement();

        if (node2.getNodeType() == Node.DOCUMENT_NODE)
            node2 = ((Document) node2).getDocumentElement();

        if (node1.getNodeType() == Node.ELEMENT_NODE) {
            NamedNodeMap attrs1 = node1.getAttributes();
            NamedNodeMap attrs2 = node2.getAttributes();
            Node child1, child2;//from  ww  w.  jav  a 2  s  . c om
            int i, j;
            for (i = 0; i < attrs1.getLength(); i++) {
                child1 = attrs1.item(i);
                for (j = 0; j < attrs2.getLength(); j++) {
                    child2 = attrs2.item(j);
                    if (child1.getNodeName().equals(child2.getNodeName())) {
                        if (!child1.getNodeValue().equals(child2.getNodeValue()))
                            System.out.println(
                                    String.format("%s -- %s \tvalue1 = %s \tvalue2 = %s", node1.getNodeName(),
                                            child1.getNodeName(), child1.getNodeValue(), child2.getNodeValue()));
                        break;
                    }
                }
                if (j == attrs2.getLength()) {
                    //System.out.println(String.format("%s -- %s \tvalue1 = %s \t value2 = !not present",node1.getNodeName(), child1.getNodeName(),child1.getNodeValue()));
                }
            }

            NodeList childs1 = node1.getChildNodes();
            NodeList childs2 = node2.getChildNodes();
            for (i = 0; i < childs1.getLength(); i++) {
                child1 = childs1.item(i);
                child2 = childs2.item(i);
                printDifferences(child1, child2);
            }
        }

    }
}

Related

  1. prettyPrintLoop(Node node, StringBuilder string, String indentation)
  2. prettyPrintNode(Node aNode, int indent)
  3. print(Node node)
  4. print(Node node)
  5. printArrayContent(Node[] t)
  6. printDOM(Node node)
  7. printDOM(Node node, String prefix)
  8. printDOMTree(Node node, PrintStream out)
  9. printElement(Node node, int level)