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?
This sounds similar to Best way to compare 2 XML documents in Java. I'd suggest checking out XMLUnit:
http://xmlunit.sourceforge.net/
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With