3

I have two xml files that I want to compare:

old.xml:

<EMPLOYEES>
  <employee>
    <id>102</id>
    <name>Fran</name>
    <department>  THIS IS COMPUTER DEPARTMENT  </department>
  </employee> 
  <employee>
    <id>105</id>
    <name>Matthew</name>
    <department> THIS IS SCIENCE AND TECHNOLOGY </department>
  </employee> 
</EMPLOYEES>

new.xml :

<EMPLOYEES>
  <employee>
    <id>105</id>
    <name>Matthew</name>
    <department>  THIS IS SCIENCE AND TECHNOLOGY **Modified *** </department>
  </employee> 
  <employee>
    <id>106</id>
    <name>xyz</name>
    <department> THIS IS SCIENCE AND TECHNOLOGY </department>
  </employee>
  <employee>
    <id>107</id>
    <name>Francis</name>
    <department>  THIS IS XYZ  </department>
  </employee>
</EMPLOYEES>

I want to compare the two files and return which records were added, deleted, or updated. old.xml contains 2 <employee> records and new.xml contains 3 <employee> records.

I'd like the results to be like this:

Added records 2 : ex:- employee.id=106 and employee.id=107

Deleted records 1 : ex:- employee.id=102

Updated records 1: ex:- employee.id=105 updated with ----

What is the best way to diff these two XML files to get these results?

2
  • Improve your formatting. I tried editing for you, but somehow it got declined. Commented Jan 10, 2013 at 22:20
  • 1
    The question is too vaguely phrased. By "I am facing some issues" do you mean you encountered a specific problem, or you just want someone to tell you how to implement detecting element updates? (It seems like you want to detect the updates by id, and there's not really a generic solution to that since it has nothing to do with the structure of the XML, but its meaning in your application.) Commented Jan 10, 2013 at 22:23

3 Answers 3

2

This sounds similar to Best way to compare 2 XML documents in Java. I'd suggest checking out XMLUnit:

http://xmlunit.sourceforge.net/

Sign up to request clarification or add additional context in comments.

Comments

1

What I would do

@XmlRootElement
class Employees {
    List<Employee> list;
}

class Employee {
    int id;
    String name;
    String department;
}

Unmarshal xmls. Create 2 maps and do following

    Map<Integer, Employee> map1 = ...
    Map<Integer, Employee> map2 = ...
                // see Map.retainAll API
    map1.keySet().retainAll(map2.keySet());
    // now map1 contains common employees
    for (Integer k : map1.keySet()) {
        Employee e1 = map1.get(k);
        Employee e2 = map2.get(k);
        // compare name and department
    }

Comments

0

For logical difference, i.e. difference between corresponding nodes in two xml files, you may use the isEqualNode(Node node) method of the node class.

For line by line comparison, Scanner is easy to use. Sample code -

    public void compareFiles (Scanner file1, Scanner file2) {
                String lineA ;
                String lineB ;

                int x = 1;

                    while (file1.hasNextLine() && file2.hasNextLine()) {
                        lineA = file1.nextLine();
                        lineB = file2.nextLine();

                        if (!lineA.equals(lineB)) {
                            System.out.print("Diff " + x++ + "\r\n");
                            System.out.print("< " + lineA + "\r\n");
                            System.out.print("> " + lineB + "\r\n");
                        }
                    }

            } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.