Java tutorial
//package com.java2s; import org.w3c.dom.Node; import java.util.*; public class Main { public static boolean compareNode(Node nQ, Node nN, Boolean considerLength, Map<String, Node> qvars) throws Exception { 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 if (!compareNode(nQ.getChildNodes().item(i), nN.getChildNodes().item(i), considerLength, qvars)) { return false; } } } } 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 { 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; } } } }