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.
cathalmchaleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.