We are parsing XML configuration files with JAXB into Java objects. The XML files are versioned and after loading version 1.0 and 2.0 into objects we would like to compare the two objects of the same but unknown type (there are many different configurations for all kinds of things) recursively and their field values and print out the differences.
An object might look as follows.
@XmlRootElement(name = "HelloWorld")
public class HelloWorldConfiguration {
private List<HelloWorldObject> helloWorldObjects = new ArrayList<HelloWorldObject>();
public HelloWorldConfiguration() {
HelloWorldObject o = new HelloWorldObject();
helloWorldObjects.add(o);
helloWorldObjects.add(o);
helloWorldObjects.add(o);
helloWorldObjects.add(o);
helloWorldObjects.add(o);
}
@XmlElement(name = "helloWorldObject")
public List<HelloWorldObject> getHelloWorldObjects() {
return helloWorldObjects;
}
public void setHelloWorldObjects(List<HelloWorldObject> helloWorldObjects) {
this.helloWorldObjects = helloWorldObjects;
}
}
public class HelloWorldObject {
private Stage firstName = new Stage("Tony");
private Stage secondName = new Stage("Stark");
public Stage getFirstName() {
return firstName;
}
public void setFirstName(Stage firstName) {
this.firstName = firstName;
}
public Stage getSecondName() {
return secondName;
}
public void setSecondName(Stage secondName) {
this.secondName = secondName;
}
}
For example we would like to be informed about following changes about the above HelloWorldConfiguration object?
- there is additional "HelloWorldObject" item in the list (the item with its attributes must be printed on screen)
- the "HelloWorldObject" at the position n has a new "firstName" value (the name of the field or XML element that changed and its value should be printed)
- the new "HelloWorldObject" list is shorter by 2 following elements (the missing elements must be printed with all attributes and values)
My questions are as follows.
- Would you solve this with reflection on the Java object level or compare the two different XML files?
- Are there any libraries out there that already do something like that for me? On XML or Java object level?
- Any examples?