Java tutorial
//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 { 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; } } } }