Reputation: 31
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?
Upvotes: 3
Views: 25677
Reputation: 1
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");
}
}
}
Upvotes: 0
Reputation: 136022
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
}
Upvotes: 1
Reputation: 10473
This sounds similar to Best way to compare 2 XML documents in Java. I'd suggest checking out XMLUnit:
http://xmlunit.sourceforge.net/
Upvotes: 2