Forum Discussion

priyanka1107's avatar
priyanka1107
Occasional Contributor
10 years ago

Comparing xml's

Can any one help me ?
Getting out of this problem.

I want to validate two xml's weather they are same or not.
I used xmlUnit jar classes for comparing this but due to miss ordering of values it is failing.

Below is all details.

reference.xml

TRAVELGUIDE

EXCURSION_AND_TICKETS

sample.xml

EXCURSION_AND_TICKETS

TRAVELGUIDE

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
import org.custommonkey.xmlunit.ElementNameAndTextQualifier;
import org.custommonkey.xmlunit.ElementNameQualifier;
import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.examples.MultiLevelElementNameAndTextQualifier;
import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
import org.xml.sax.SAXException;

public class ElementNameAttrDiffTest {

public static void main(String[] args) {
URL url1 = ElementNameAttrDiffTest.class.getResource(“reference.xml”);
URL url2 = ElementNameAttrDiffTest.class.getResource(“sample.xml”);
FileReader fr1 = null;
FileReader fr2 = null;
try {
fr1 = new FileReader(url1.getPath());
fr2 = new FileReader(url2.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
XMLUnit.setIgnoreWhitespace(Boolean.TRUE);
XMLUnit.setIgnoreAttributeOrder(true);
try {
Diff diff = new Diff(fr1, fr2);
System.out.println(“Similar? ” + diff.similar());
System.out.println(“Identical? ” + diff.identical());

DetailedDiff detDiff = new DetailedDiff(diff);
detDiff.overrideMatchTracker(new MatchTrackerImpl());
//detDiff.overrideElementQualifier(new ElementNameQualifier());
// detDiff.overrideElementQualifier(new ElementNameAndAttributeQualifier());
detDiff.overrideElementQualifier(new MultiLevelElementNameAndTextQualifier(2));
List differences = detDiff.getAllDifferences();
for (Object object : differences) {
Difference difference = (Difference)object;
System.out.println(“***********************”);
System.out.println(difference);
System.out.println(“***********************”);
}

} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.MatchTracker;
import org.custommonkey.xmlunit.NodeDetail;
import org.w3c.dom.Node;

class MatchTrackerImpl implements MatchTracker {

public void matchFound(Difference difference) {
if (difference != null) {
NodeDetail controlNode = difference.getControlNodeDetail();
NodeDetail testNode = difference.getTestNodeDetail();

String controlNodeValue = printNode(controlNode.getNode());
String testNodeValue = printNode(testNode.getNode());

if (controlNodeValue != null) {
System.out.println(“####################”);
System.out.println(“Control Node: ” + controlNodeValue);
}
if (testNodeValue != null) {
System.out.println(“Test Node: ” + testNodeValue);
System.out.println(“####################”);
}
}
}

private static String printNode(Node node) {
if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, “yes”);
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
System.out.println(“nodeToString Transformer Exception”);
}
return sw.toString();

}
return null;
}

}

Similar? false
Identical? false
####################

Expected sequence of child nodes ’0′ but was ’1′ – comparing at /UsernameToken[1]/OtherSrvcPref[1] to at /UsernameToken[1]/OtherSrvcPref[2]
***********************
***********************
Expected sequence of child nodes ’1′ but was ’0′ – comparing at /UsernameToken[1]/OtherSrvcPref[2] to at /UsernameToken[1]/OtherSrvcPref[1]

above are the xml’s and the code which i am using.

How i will overcome/ignore the sequence of Nodes.?

2 Replies