Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How to compare two similar xml documents ignoring node text values by using XMLUnit?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two different XML documents below and please note that they are having the same basic structure (schema).

Source XML


Test XML


And I build this snippet function to compare those two XML documents.



And here is the Maven dependency


I am expecting those two XML documents are the same, but the function always returns false. Are there any ways that I can ignore the node text value when comparing two XML structures?
As you can see, I already used IgnoreTextAndAttributeValuesDifferenceListener, but I still got the problem.

 
Saloon Keeper
Posts: 12994
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

I'm not familiar with XMLUnit, but could it be that you have to set options like whether to normalize whitespace, before you parse the documents?

At any rate, you may first want to debug the documents you parsed by checking the properties of docSource and docTest and see where they are different.
 
Ranch Hand
Posts: 733
7
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a fundamental and conceptual difference between <description>String</description> and <description></description> which is that the length (via say .getLength()) of the child nodes being 1 for the former whereas being 0 in the latter. If you check the source code of IgnoreTextAndAttributeValuesDifferenceListener class, you would see that that length is being checked and any difference would be considered as a Difference - for good reason, though, but it would result in some inconvenience to the user in use case like yours.

I already used IgnoreTextAndAttributeValuesDifferenceListener, but I still got the problem.


That setting points to a different functionality. If it were checking <description>String</description> and <description>Str</description>, it would be fine and a match would be obtained. But not the case as discussed above.

Some would take the approach of writing one's own Listener. I can point you to an example:
https://stackoverflow.com/questions/28737977/ignore-text-differences-when-comparing-xml-with-xmlunit
The way of writing it is modeling the source code of IgnoreTextAndAttributeValuesDifferenceListener and just modifying the bit of checking getLength bit mentioned above. I see no reason to repeat those kinds of answer, probably repeated all over.

I can propose my approach which is based on preprocessor using xslt. I think this approach is more powerful and versatile and conceptually clearer. XMLUnit has built-in xslt support which actually tacitly aims at resolving a much bigger classes of differences not easily resolved using the build-in Listeners and/or their extension by subclassing. Differences between xml files and/or text files, ... are difficult problem in general, mathematically like ... (I refrain from putting a name of some topic, risking to make people wondering), so vast but not necessarily very rewarding !

Here is how.

First you write a short xslt stylesheet, let's say, _ignoreText.xsl placing in the right directory of course, like this.


Then you modify your classes, adding import of org.custommonkey.xmlunit.Transform and also catching javax.xml.transform.TransformerException.

Then you are good to go.
 
I am Arthur, King of the Britons. And this is a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic