Link to home
Start Free TrialLog in
Avatar of cathalmchale
cathalmchale

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of lhankins
lhankins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
Yes, i think you'll have to do this 'manually' and recursively
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sudhakar_koundinya
sudhakar_koundinya

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;

  }

}

You can test like this

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

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

lhankins' idea ;-)
Avatar of cathalmchale

ASKER

Thanks for your posts, this works very nicely ;-)
:-)