Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1133
  • Last Modified:

JDOM - compare 2 Element's

Hi there,

does anyone use the JDOM api?
I want to compare 2 different elements to see if they are the same.
- same = same number of children;  children have the same number of children;  the element and attributes of each child have the same value!!

Im guessing this is a recursive nightmare (especially considering that the itteration of element 1 must always be at same point as that of element 2).
So has it been done before!!

Cheers,
Cathal.
0
cathalmchale
Asked:
cathalmchale
2 Solutions
 
lhankinsCommented:
One way to do it would be to convert everything under the two elements into XML strings, then compare the two strings.   This wouldn't be the most effecient way, but should work...
0
 
CEHJCommented:
Yes, i think you'll have to do this 'manually' and recursively
0
 
CEHJCommented:
>>One way to do it would be to convert everything under the two elements into XML strings

The trouble with that is that inessential differences could make the comparision invalid when it shouldn't be, but it's certainly more convenient than doing your own recursion. Something like :

StringWriter sw1 = new StringWriter();
StringWriter sw2 = new StringWriter();
new XMLOutputter().output(element1, sw1);
new XMLOutputter().output(element2, sw2);
boolean elementsEqual = sw1.toString().equals(sw2.toString());
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sudhakar_koundinyaCommented:
Not tested thoroughly, but might give u some idea

package org.prithvi.test;

import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;

public class JDomTest {
  public JDomTest() {
  }

  public static void main(String[] args) {
    try {
      Document d = new SAXBuilder().build(new File("c:/1.xml"));
      Document d1 = new SAXBuilder().build(new File("c:/11.xml"));

      Element e = d.getRootElement();
      Element e1 = d1.getRootElement();
      check(e, e1);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static boolean check(Element e, Element e1) {
    List l = e.getChildren();
    List l1 = e1.getChildren();
    if (l.size() != l1.size()) {
      return false;
    }

    boolean b = true;
    b = e.getName().equals(e1.getName());
    if (b) {
      b = e.getValue().equals(e1.getValue());
      if (!b) {
        return false;
      }
    }
    else {
      return false;
    }
    for (int i = 0; i < l.size(); i++) {
      Element e2 = (Element) l.get(i);
      Element e3 = (Element) l1.get(i);

      List att1 = e2.getAttributes();
      List att2 = e3.getAttributes();
      if (att1.size() != att2.size()) {
        return false;
      }
      for (int j = 0; j < att1.size(); j++) {
        Attribute at1 = (Attribute) att1.get(j);
        Attribute at2 = (Attribute) att2.get(j);
        b = at1.getValue().equals(at2.getValue());
        if (b) {
          b = at1.getName().equals(at2.getName());
          if (!b) {
            return false;
          }
        }
        else {
          return false;
        }
      }
      if (e2.getText().equals(e3.getText())) {
        b = check(e2, e3);
        if (!b) {
          return false;
        }
      }
      else {
        return false;
      }

    }
    return true;

  }

}

0
 
sudhakar_koundinyaCommented:
You can test like this

if (check(e, e1)) {
        System.err.println("Same Xml files");
      }
      else {
        System.err.println("Different Xml files");
      }
0
 
sudhakar_koundinyaCommented:
CEHJ,

your's is much simpler than my code. Easy to understand and no recursion. Good One ;-)
0
 
CEHJCommented:
>>your's is much simpler than my code. Easy to understand and no recursion. Good One ;-)

lhankins' idea ;-)
0
 
cathalmchaleAuthor Commented:
Thanks for your posts, this works very nicely ;-)
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now