Java XML Node Compare compareNode(Node nQ, Node nN, Boolean considerLength, Map qvars)

Here you can find the source of compareNode(Node nQ, Node nN, Boolean considerLength, Map qvars)

Description

compare Node

License

Apache License

Declaration

public static boolean compareNode(Node nQ, Node nN, Boolean considerLength, Map<String, Node> qvars)
            throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Node;

import java.util.Map;

public class Main {
    public static boolean compareNode(Node nQ, Node nN, Boolean considerLength, Map<String, Node> qvars)
            throws Exception {
        /*System.out.println("current query tree:");
        try {/*from   w  w  w. j a v  a 2 s  .  com*/
         System.out.println(printDocument(nQ));
        }catch (Exception e) {
         e.printStackTrace();
        }
        System.out.println("current comp tree:");
        try {
         System.out.println(printDocument(nN));
        }catch (Exception e) {
         e.printStackTrace();
        }// END OF DEBUG output XML */
        if (qvars == null) {
            throw new Exception("qvars array must not be null");
        }
        if (nQ.hasChildNodes()) {
            int nQChildLength = nQ.getChildNodes().getLength();
            if (nN.hasChildNodes() && (!considerLength || nQChildLength == nN.getChildNodes().getLength())) {
                //loop through all childnodes
                for (int i = 0; i < nQChildLength; i++) {
                    //System.out.println("recurse to "+ nQ.getChildNodes().item( i )+"vs"+nN.getChildNodes().item( i )); //DEBUG output XML
                    if (!compareNode(nQ.getChildNodes().item(i), nN.getChildNodes().item(i), considerLength,
                            qvars)) {
                        return false;
                    }
                }
            }
        }
        //check for qvar descendant, add to qvar hashmap for checking (required for checking multiple qvars)
        if (nQ.getNodeName().equals("mws:qvar")) {
            String qvarName = nQ.getAttributes().getNamedItem("name").getNodeValue();
            if (qvars.containsKey(qvarName)) {
                return compareNode(qvars.get(qvarName), nN, considerLength, qvars);
            } else {
                qvars.put(qvarName, nN);
                return true;
            }
        } else {
            //Attributes are ignored; child nodelists are not equal in length and considerlength is false OR reached lowest level: therefore check nodevalue
            if (nQ.getNodeName().equals(nN.getNodeName())) {
                try {
                    return nQ.getNodeValue().trim().equals(nN.getNodeValue().trim());
                } catch (NullPointerException e) {
                    //NodeValue does not exist
                    return true;
                }
            } else {
                return false;
            }
        }
    }
}

Related

  1. areEqual(Node left, Node right)
  2. compare(Node n1, Node n2)
  3. compareLocations(Node node1, int offset1, Node node2, int offset2)
  4. compareNodes(Node expected, Node actual)
  5. compareStringNode(Node node, String tag)
  6. compareTwoNodes(Node m, Node n)
  7. equalNode(Node nodeA, Node nodeB)