Compare two DOM Nodes from JBoss : Node « XML « Java Tutorial

 * JBoss, Home of Professional Open Source
 * Distributable under LGPL license.
 * See terms of license at

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

 * @author <a href="">Ovidiu Feodorov</a>
 * @version <tt>$Revision: 3282 $</tt> $Id: 3282 2007-11-01
 *          15:32:29Z timfox $
public class Utils {

  public static void assertEquivalent(Node node, Node node2) {
    if (node == null) {
      throw new IllegalArgumentException("the first node to be compared is null");

    if (node2 == null) {
      throw new IllegalArgumentException("the second node to be compared is null");

    if (!node.getNodeName().equals(node2.getNodeName())) {
      throw new IllegalArgumentException("nodes have different node names");

    int attrCount = 0;
    NamedNodeMap attrs = node.getAttributes();
    if (attrs != null) {
      attrCount = attrs.getLength();

    int attrCount2 = 0;
    NamedNodeMap attrs2 = node2.getAttributes();
    if (attrs2 != null) {
      attrCount2 = attrs2.getLength();

    if (attrCount != attrCount2) {
      throw new IllegalArgumentException("nodes hava a different number of attributes");

    outer: for (int i = 0; i < attrCount; i++) {
      Node n = attrs.item(i);
      String name = n.getNodeName();
      String value = n.getNodeValue();

      for (int j = 0; j < attrCount; j++) {
        Node n2 = attrs2.item(j);
        String name2 = n2.getNodeName();
        String value2 = n2.getNodeValue();

        if (name.equals(name2) && value.equals(value2)) {
          continue outer;
      throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match");

    boolean hasChildren = node.hasChildNodes();

    if (hasChildren != node2.hasChildNodes()) {
      throw new IllegalArgumentException("one node has children and the other doesn't");

    if (hasChildren) {
      NodeList nl = node.getChildNodes();
      NodeList nl2 = node2.getChildNodes();

      short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE };
      List nodes = filter(nl, toFilter);
      List nodes2 = filter(nl2, toFilter);

      int length = nodes.size();

      if (length != nodes2.size()) {
        throw new IllegalArgumentException("nodes hava a different number of children");

      for (int i = 0; i < length; i++) {
        Node n = (Node) nodes.get(i);
        Node n2 = (Node) nodes2.get(i);
        assertEquivalent(n, n2);

  private static List filter(NodeList nl, short[] typesToFilter) {
    List nodes = new ArrayList();

    outer: for (int i = 0; i < nl.getLength(); i++) {
      Node n = nl.item(i);
      short type = n.getNodeType();
      for (int j = 0; j < typesToFilter.length; j++) {
        if (typesToFilter[j] == type) {
          continue outer;
    return nodes;

33.29.1.Add a text node to the element
33.29.2.Add a text node to the beginning of the element
33.29.3.Add a text node before the last child of the element
33.29.4.Add a text node in front of the new item element
33.29.5.Adding a Text Node to a DOM Document
33.29.6.Removing a Node from a DOM Document
33.29.7.Remove All nodes
33.29.8.Get the text node
33.29.9.Change a particular node in XML
33.29.10.Add another element after the first child of the root element
33.29.11.Create a new element and move the middle text node to it
33.29.12.Move all children of the element in front of the element
33.29.13.Split the node at the beginning of the word
33.29.14.Compare two DOM Nodes from JBoss
33.29.15.Convert NodeList To Node Array
33.29.16.Convert node element To String
33.29.17.Search our next siblings for a given node
33.29.18.Search earlier siblings for a given node
33.29.19.Returns a first child DOM Node of type ELEMENT_NODE for the specified Node
33.29.20.Search up the tree for a given node
33.29.21.Get the first text node associated with this element
33.29.22.Simplified implementation of a Node from a Document Object Model (DOM)
33.29.23.Returns a list of value for the given node
33.29.24.Returns the value of the child node with the given name
33.29.25.Remove this node from its parent.
33.29.26.Find the first text descendent node of an element
33.29.27.Copies the source tree into the specified place in a destination tree.
33.29.28.DOM helper for root element
33.29.29.Compare two DOM Nodes