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.
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.
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: