1

I have two different XML documents below and please note that they are having the same basic structure (schema).

Source XML

<root>
    <name>String</name>
    <description>String</description>
</root>

Test XML

<root>
    <name>Test</name>
    <description></description> <!-- it is an empty node -->
</root>

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

import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.Difference;
import org.custommonkey.xmlunit.IgnoreTextAndAttributeValuesDifferenceListener;
import org.custommonkey.xmlunit.XMLUnit;

public static void main(String args[]) throws FileNotFoundException,
            SAXException, IOException, ParserConfigurationException, XPathExpressionException {

        String strSource = "<root><name>String</name><description>String</description></root>";
        String strTest = "<root><name>Test</name><description></description></root>";

        Document docSource = stringToXMLDocument(strSource);
        Document docTest = stringToXMLDocument(strTest);

        boolean result = isMatched(docSource, docTest);
        if(result){
            System.out.println("Matched!");
        }else{
            System.out.println("Un-matched!");
        }
    }
public static boolean  isMatched(Document xmlSource, Document xmlCompareWith) {
        XMLUnit.setIgnoreWhitespace(true);
        XMLUnit.setIgnoreComments(true);
        XMLUnit.setIgnoreAttributeOrder(true);

        XMLUnit.setNormalizeWhitespace(true);
        XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

        Diff myDiff = new Diff(xmlSource, xmlCompareWith);
        myDiff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
        return myDiff.similar();
    }

public static Document stringToXMLDocument(String str) throws   ParserConfigurationException, SAXException, IOException{
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(true);

    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document document = docBuilder.parse(new InputSource(new StringReader(str)));

    return document;
}

And here is the Maven dependency

<dependency>
  <groupId>xmlunit</groupId>
  <artifactId>xmlunit</artifactId>
  <version>1.6</version>
</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.

NoName
  • 877
  • 12
  • 28
  • I looked around a bit to agree there doesn't seem to be a builtin solution. The problem I think isn't that you're not ignoring text node differences... it's that there's no text node at all in ``. Since XMLUnit 2.x is out with some significant changes https://github.com/xmlunit/user-guide/wiki/Migrating-from-XMLUnit-1.x-to-2.x ..maybe it'd help someone who really knew what they were doing if you indicated whether you'd be open to upgrading. – Scott Kurz Mar 02 '21 at 20:47
  • I am now overwriting IgnoreTextAndAttributeValuesDifferenceListener.differenceFound() to solve my case. – NoName Mar 04 '21 at 04:47

1 Answers1

1

You may need to provide a DifferenceListener of your own that delegates to IgnoreTextAndAttributeValuesDifferenceListener and in addition handles differences of type HAS_CHILDNODES and CHILD_NODELIST_LENGTH.

As @scott-kurz pointed out in the cmments there may not be any XML Text node at all, rather than an empty one, depending on your XML parser and its configuration.

Stefan Bodewig
  • 3,260
  • 15
  • 22