Here you can find the source of equals(Node n1, Node n2)
Parameter | Description |
---|---|
n1 | first Node to test |
n2 | second Node to test |
public static boolean equals(Node n1, Node n2)
//package com.java2s; //License from project: Open Source License import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; public class Main { /**//ww w.j a va2s . c o m * @param n1 first Node to test * @param n2 second Node to test * @return true if a deep compare show the same children and attributes in the same order */ public static boolean equals(Node n1, Node n2) { // compare type if (!n1.getNodeName().equals(n2.getNodeName())) return false; // compare attributes NamedNodeMap nnm1 = n1.getAttributes(); NamedNodeMap nnm2 = n2.getAttributes(); if (nnm1.getLength() != nnm2.getLength()) return false; for (int i = 0; i < nnm1.getLength(); i++) { Node attr1 = nnm1.item(i); if (!getAttribute(n1, attr1.getNodeName()).equals(getAttribute(n2, attr1.getNodeName()))) return false; } // compare children Node c1 = n1.getFirstChild(); Node c2 = n2.getFirstChild(); for (;;) { while ((c1 != null) && c1.getNodeName().startsWith("#")) c1 = c1.getNextSibling(); while ((c2 != null) && c2.getNodeName().startsWith("#")) c2 = c2.getNextSibling(); if ((c1 == null) && (c2 == null)) break; if ((c1 == null) || (c2 == null)) return false; if (!equals(c1, c2)) return false; c1 = c1.getNextSibling(); c2 = c2.getNextSibling(); } return true; } /** * @param n Node to examine * @param attr Attribute to look for * @param def Default value to return if attribute is not present * @return if the Node contains the named Attribute, the value, if not, the def parameter */ public static String getAttribute(Node n, String attr, String def) { NamedNodeMap attrs = n.getAttributes(); if (attrs == null) return def; Node ret = attrs.getNamedItem(attr); if (ret == null) return def; else return ret.getNodeValue(); } /** * @param n Node to examine * @param attr Attribute to look for * @return if the Node contains the named Attribute, the value, if not, empty string */ public static String getAttribute(Node n, String attr) { return getAttribute(n, attr, ""); } }