Hello,
I am using these two classes to ignore the nodes for comparision in xml
unit. I am unsuccessful still to eliminate the element with City.
Please find the code below
*
package* com.poc;
*
import* java.io.FileNotFoundException;
*
import* java.io.FileInputStream;
*
import* java.io.FileReader;
*
import* java.io.IOException;
*
import* *java.util.HashSet*;
*
import* java.util.List;
*
import* *java.util.Set*;
*
import* org.custommonkey.xmlunit.DetailedDiff;
*
import* org.custommonkey.xmlunit.Diff;
*
import* org.custommonkey.xmlunit.Difference;
*
import* *org.custommonkey.xmlunit.DifferenceConstants*;
*
import* org.custommonkey.xmlunit.DifferenceEngine;
*
import* *org.custommonkey.xmlunit.DifferenceListener*;
*
import* org.custommonkey.xmlunit.XMLUnit;
*
import* *org.w3c.dom.Node*;
*
import* org.xml.sax.SAXException;
*
public* *class* xmlUnitutility {
*public* *static* *void* main(String[] args) {
String fn1 = "Data/input.xml";
String fn2 = "Data/expected.xml";
String ignore = "City";
XMLUnit.*setNormalizeWhitespace*(Boolean.*TRUE*);
XMLUnit.*setIgnoreDiffBetweenTextAndCDATA*(Boolean.*TRUE*);
XMLUnit.*setIgnoreComments*(*true*);
XMLUnit.*setIgnoreAttributeOrder*(*true*);
Ignoreelement ie = *new* Ignoreelement(ignore);
*try* {
FileInputStream *in1* = *new* FileInputStream(fn1);
} *catch* (FileNotFoundException e1) {
// *TODO* Auto-generated catch block
e1.printStackTrace();
}
*try* {
FileInputStream *in2* = *new* FileInputStream(fn2);
} *catch* (FileNotFoundException e1) {
// *TODO* Auto-generated catch block
e1.printStackTrace();
}
FileReader fr1 = *null*;
FileReader fr2 = *null*;
*try* {
fr1 = *new* FileReader(fn1);
fr2 = *new* FileReader(fn2);
} *catch* (FileNotFoundException e) {
e.printStackTrace();
}
*try* {
Diff diff = *new* Diff(fr1, fr2);
System.*out*.println("Similar? " + diff.similar());
System.*out*.println("Identical? " + diff.identical());
DetailedDiff detDiff = *new* DetailedDiff(diff);
*List* differences = detDiff.getAllDifferences();
*for* (Object object : differences) {
Difference difference = (Difference)object;
DifferenceEngine *engine* = *new* DifferenceEngine(diff);
// ie.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
ie.differenceFound(difference);
// diff.overrideDifferenceListener(*ie*);
System.*out*.println("***********************");
System.*out*.println(difference);
System.*out*.println("***********************");
}
} *catch* (SAXException e) {
e.printStackTrace();
} *catch* (IOException e) {
e.printStackTrace();
}
}
}
Class which implemented Difference Listener
*
package* com.poc;
*
import* java.io.FileNotFoundException;
*
import* java.io.FileInputStream;
*
import* java.io.FileReader;
*
import* java.io.IOException;
*
import* *java.util.HashSet*;
*
import* java.util.List;
*
import* *java.util.Set*;
*
import* org.custommonkey.xmlunit.DetailedDiff;
*
import* org.custommonkey.xmlunit.Diff;
*
import* org.custommonkey.xmlunit.Difference;
*
import* *org.custommonkey.xmlunit.DifferenceConstants*;
*
import* org.custommonkey.xmlunit.DifferenceEngine;
*
import* *org.custommonkey.xmlunit.DifferenceListener*;
*
import* org.custommonkey.xmlunit.XMLUnit;
*
import* *org.w3c.dom.Node*;
*
import* org.xml.sax.SAXException;
*
public* *class* xmlUnitutility {
*public* *static* *void* main(String[] args) {
String fn1 = "Data/input.xml";
String fn2 = "Data/expected.xml";
String ignore = "City";
XMLUnit.*setNormalizeWhitespace*(Boolean.*TRUE*);
XMLUnit.*setIgnoreDiffBetweenTextAndCDATA*(Boolean.*TRUE*);
XMLUnit.*setIgnoreComments*(*true*);
XMLUnit.*setIgnoreAttributeOrder*(*true*);
Ignoreelement ie = *new* Ignoreelement(ignore);
*try* {
FileInputStream *in1* = *new* FileInputStream(fn1);
} *catch* (FileNotFoundException e1) {
// *TODO* Auto-generated catch block
e1.printStackTrace();
}
*try* {
FileInputStream *in2* = *new* FileInputStream(fn2);
} *catch* (FileNotFoundException e1) {
// *TODO* Auto-generated catch block
e1.printStackTrace();
}
FileReader fr1 = *null*;
FileReader fr2 = *null*;
*try* {
fr1 = *new* FileReader(fn1);
fr2 = *new* FileReader(fn2);
} *catch* (FileNotFoundException e) {
e.printStackTrace();
}
*try* {
Diff diff = *new* Diff(fr1, fr2);
System.*out*.println("Similar? " + diff.similar());
System.*out*.println("Identical? " + diff.identical());
DetailedDiff detDiff = *new* DetailedDiff(diff);
*List* differences = detDiff.getAllDifferences();
*for* (Object object : differences) {
Difference difference = (Difference)object;
DifferenceEngine *engine* = *new* DifferenceEngine(diff);
// ie.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
ie.differenceFound(difference);
// diff.overrideDifferenceListener(*ie*);
System.*out*.println("***********************");
System.*out*.println(difference);
System.*out*.println("***********************");
}
} *catch* (SAXException e) {
e.printStackTrace();
} *catch* (IOException e) {
e.printStackTrace();
}
}
}
Please help
Thanks
Rajesh
------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA
http://p.sf.net/sfu/rim-devcon-copy2
_______________________________________________
Xmlunit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/xmlunit-general