Java Utililty Methods XML Node Compare

List of utility methods to do XML Node Compare

Description

The list of methods to do XML Node Compare are organized into topic(s).

Method

booleanareEqual(Node left, Node right)
are Equal
return left.isEqualNode(right);
booleancompare(Node n1, Node n2)
Compares two DOM nodes for (deep) equality.
boolean ret = true;
ret &= n1.getLocalName().equals(n2.getLocalName());
ret &= n1.getNamespaceURI().equals(n2.getNamespaceURI());
String text = n1.getTextContent();
if (text != null) {
    ret &= text.equals(n2.getTextContent());
NodeList children = n1.getChildNodes();
...
intcompareLocations(Node node1, int offset1, Node node2, int offset2)
compare two nodes.
if (node1 == node2) {
    return offset1 - offset2;
List parents1 = getParents(node1);
List parents2 = getParents(node2);
parents1.add(0, node1);
parents2.add(0, node2);
if (node1.hasChildNodes()) {
...
booleancompareNode(Node nQ, Node nN, Boolean considerLength, Map qvars)
compare Node
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())) {
        for (int i = 0; i < nQChildLength; i++) {
            if (!compareNode(nQ.getChildNodes().item(i), nN.getChildNodes().item(i), considerLength,
...
voidcompareNodes(Node expected, Node actual)
compare Nodes
if (expected.getNodeType() != actual.getNodeType()) {
    throw new Exception("Different types of nodes: " + expected + " " + actual);
if (expected instanceof Document) {
    Document expectedDoc = (Document) expected;
    Document actualDoc = (Document) actual;
    compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement());
} else if (expected instanceof Element) {
...
booleancompareStringNode(Node node, String tag)
Checks if a node resembles the given XML tag (including attributes).
if (tag.indexOf(node.getNodeName()) == -1)
    return false;
NamedNodeMap attributes = node.getAttributes();
if (attributes != null) {
    for (int i = 0; i < attributes.getLength(); i++) {
        if (tag.indexOf(attributes.item(i).getNodeName()) == -1)
            return false;
        if (tag.indexOf(attributes.item(i).getNodeValue()) == -1)
...
booleancompareTwoNodes(Node m, Node n)
compare Two Nodes
return nodesAreEqual(m.getLocalName(), n.getLocalName()) && nodesAreEqual(m.getPrefix(), n.getPrefix());
booleanequalNode(Node nodeA, Node nodeB)
equal Node
if (nodeA == null) {
    if (nodeB == null) {
        return true;
    return false;
if (nodeA.getNodeType() != nodeB.getNodeType()) {
    return false;
...
booleanequals(Node n1, Node n2)
equals
if (!n1.getNodeName().equals(n2.getNodeName()))
    return false;
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);
...
booleanisDescendant(Node testNode, Node compareToNode)
is Descendant
if (testNode != null && compareToNode != null) {
    while (testNode.getParentNode() != null) {
        if (testNode.getParentNode() == compareToNode) {
            return true;
        testNode = testNode.getParentNode();
return false;