3

I have an issue trying to compare 2 XML documents in Java, using oracle.xml.differ.XMLDiff. The code is fully implemented and I expected it to be working fine, until I discovered an attribute change is not picked up in some instances. To demonstrate this, I have the following:

Setup:

DOMParser parser = new DOMParser();
parser.setPreserveWhitespace(false);
parser.parse(isCurrent);
XMLDocument currentXmlDoc = parser.getDocument();

parser.parse(isPrior);
XMLDocument priorXmlDoc = parser.getDocument();

XMLDiff xmlDiff = new XMLDiff();
xmlDiff.setDocuments(currentXmlDoc, priorXmlDoc);

In the first case, the attribute change in Strike is picked up fine. I have the following 2 XML files:

XML1

<Periods>
   <Period Start="2011-03-28" End="2011-04-17" AverageStart="" AverageEnd="" Notional="6000000.0000" OptionType="Swap" Payment="2011-04-19" Strike="72.0934800" Underlying="ZA" ResetStrike="No" ResetNotional="No" QuotingDate="2011-04-17" Multiplier="1.000000" PlusConstant="0.000000" StopLossPercent="" StopLossLevel=""/>
</Periods>

XML2

<Periods>
   <Period Start="2011-03-28" End="2011-04-17" AverageStart="" AverageEnd="" Notional="6000000.0000" OptionType="Swap" Payment="2011-04-19" Strike="0.0000000" Underlying="ZA" ResetStrike="No" ResetNotional="No" QuotingDate="2011-04-17" Multiplier="1.000000" PlusConstant="0.000000" StopLossPercent="" StopLossLevel=""/>
</Periods>

In the second case, the attribute change in Strike is not picked up. I have the following 2 XML files:

XML1

<Periods>
    <Period Start="2011-03-28" End="2011-04-30" Payment="2011-05-02" Notional="5220000.000000" Strike="176.201900" StopLossPercent="" StopLossLevel=""/>
</Periods>

XML2

<Periods>
    <Period Start="2011-03-28" End="2011-04-30" Payment="2011-05-02" Notional="5220000.000000" Strike="0.000000" StopLossPercent="" StopLossLevel=""/>
</Periods>

Does anyone know if I'm doing something wrong, or is there a bug in the XMLDiff package?

Alternatively, does anyone know a different tool that can be used in the same way, just identifying differences in nodes and attributes between XML files, regardless of the order?

Thanks, Milena

UPDATE: As it's extremely time-consuming to get new external packages approved for use in our system, in the ideal case I'd like to find a solution to making oracle.xml.differ.XMLDiff work. Obviously if there really is a bug and this can't be bypassed I'll consider other tools.

UPDATE 2: Since nobody seems to know about the XMLDiff bug, I'll try implementing the suggested XMLUnit package, it should do the trick.

2
  • Just to ask the obvious - are you sure that isPrior and isCurrent are pointing to the correct source? Commented Apr 12, 2013 at 16:28
  • Yes, definitely - as a part of this test, I also use printDiffTree that prints out one XML and points out any differences vs. the other XML file. So it shows me that in the second case, the input file is correct, but does not highlight the difference versus the other file. In the first case, printDiffTree shows exactly the modified attribute. Other suggestions? Commented Apr 15, 2013 at 8:48

1 Answer 1

1

In a unit test i'm using org.custommonkey.xmlunit.Diff for comparing xml content. See http://xmlunit.sourceforge.net/api/org/custommonkey/xmlunit/Diff.html

I'm comparing xml strings but you can also compare xml w3c documents. I hope you can convert your XMLDocument to either a String of an org.w3c.dom.Document.

my testcase looks like this:

String actualXML = SomeClass.getElement().asXML();
String expectedXML = IOUtils.toString(this.getClass().getResourceAsStream("/expected.xml"));

org.custommonkey.xmlunit.Diff myDiff = new Diff(StringUtils.deleteWhitespace(expectedXML), StringUtils.deleteWhitespace(actualXML));
assertTrue(MessageFormat.format("XML must be simular: {0}\nActual XML:\n{1}\n", myDiff, actualXML), myDiff.similar());

p.s. I also use the apache commons StringUtils.deleteWhitespace() method, cause i'm not interested in white space differences.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for letting me know about this tool. A couple of questions though - is this only to be used for unit testing? And can I see what exactly changed between two XMLs? Because I actually need to use this in my main code to identify if the change happened, and if so, which node/attribute it is exactly and what the old/new values are. I've taken a look at the link you sent, I can see the method differenceFound(Difference difference) and the possibility to override it with overrideDifferenceListener(DifferenceListener delegate), but an example of this would be useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.