[jdom-interest] XML "diff"
Kenneth Ellefsen
kellefsen at bergen.oilfield.slb.com
Tue Mar 18 23:12:51 PST 2003
Hi.
Heres something I made a while ago to create diffs.
Works fine in my case where el2 is the root of a document that is a
subset of the full document that el1 is the root of.
/**
* This method is recursive.
* @param el1 Root of the full "document"
* @param el2 Root of the subset "document"
* @return The root of the diff "document"
*/
private Element diff(Element el1, Element el2){
Element el = null;
if(!equals(el1, el2)){
el = new Element(el2.getName());
java.util.List a = el2.getAttributes();
int size = a.size();
for(int i=0; i<size; i++)
el.setAttribute( (Attribute) ( (Attribute)a.get(i) ).clone());
}
java.util.List l1 = el1.getChildren();
java.util.List l2 = el2.getChildren();
Iterator li1 = l1.iterator();
Iterator li2 = l2.iterator();
while(li1.hasNext()){
Element e = diff((Element)li1.next(), (Element)li2.next());
if(e != null){
if(el == null){
el = new Element(el2.getName());
java.util.List a = el2.getAttributes();
int size = a.size();
for(int i=0; i<size; i++)
el.setAttribute((Attribute)((Attribute)a.get(i)).clone());
}
el.addContent(e);
}
}
return el;
}
private boolean equals(Element el1, Element el2){
java.util.List l1 = el1.getAttributes();
java.util.List l2 = el2.getAttributes();
Iterator li1 = l1.iterator();
while(li1.hasNext()){
Iterator li2 = l2.iterator();
Attribute at1 = (Attribute)li1.next();
Attribute at2 = (Attribute)li2.next();
while(!at1.getName().equals(at2.getName())&&li2.hasNext())
at2 = (Attribute)li2.next();
if(!at1.getValue().equals(at2.getValue())){
return false;
}
}
return true;
}
More information about the jdom-interest
mailing list