Java Utililty Methods XML Node Compare

List of utility methods to do XML Node Compare

  1. HOME
  2. Java
  3. X
  4. XML Node Compare

Description

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

Method

boolean areEqual(Node left, Node right)
are Equal
return left.isEqualNode(right);
boolean compare(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();
...
int compareLocations(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()) {
...
boolean compareNode(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,
...
void compareNodes(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) {
...
boolean compareStringNode(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)
...
boolean compareTwoNodes(Node m, Node n)
compare Two Nodes
return nodesAreEqual(m.getLocalName(), n.getLocalName()) && nodesAreEqual(m.getPrefix(), n.getPrefix());
boolean equalNode(Node nodeA, Node nodeB)
equal Node
if (nodeA == null) {
 if (nodeB == null) {
 return true;
 return false;
if (nodeA.getNodeType() != nodeB.getNodeType()) {
 return false;
...
boolean equals(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);
...
boolean isDescendant(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;


AltStyle によって変換されたページ (->オリジナル) /