XMLUnit - Ignoring 'id' attribute in comparison
Asked Answered
F

2

9

I am currently working with XMLUnit and I am wondering if there is way to configure it to ignore only the id attribute of the tags I want to compare.

Thanks in advance for your help.

Fargone answered 9/3, 2011 at 16:40 Comment(1)
See #1242093Edifice
F
1

I am gonna try to implement my own DifferenceListener to handle this need (see this post). Gonna post the result soon. It seems to work, gonna post the implementation tomorrow.

Fargone answered 9/3, 2011 at 17:3 Comment(2)
could you send example, please?Derina
You should take a look at this post (#1242093) it's pretty much like the listener I implemented.Fargone
A
8

The solution is quite simple. You can configure your DifferenceEngine to handle ATTR_VALUE differences. Write custom difference listener class which implements DifferenceListener:

class IgnoreIDsDifferenceListener implements DifferenceListener {
    private static final int[] IGNORE_VALUES = new int[] {
            DifferenceConstants.ATTR_VALUE.getId(),
    };

    private boolean isIgnoredDifference(Difference difference) {
        int differenceId = difference.getId();
        for (int i=0; i < IGNORE_VALUES.length; ++i) {
            if (differenceId == IGNORE_VALUES[i]) {
                return true;
            }
        }
        return false;
    }

    public int differenceFound(Difference difference) {
        if (isIgnoredDifference(difference)) {
            return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
        } else {
            return RETURN_ACCEPT_DIFFERENCE;
        }
    }

    public void skippedComparison(Node control, Node test) {
    }
}

The required thing here is to check whether the attribute name is "id". Standard Java DOM functionality could help. But I prefer to do this by the means of regular expressions:

String controlNode = difference.getControlNodeDetail().getNode().toString();
controlNode .matches("^id=\".*\"")

P.S. See also: http://xmlunit.sourceforge.net/api/org/custommonkey/xmlunit/Difference.html

Adai answered 6/7, 2012 at 10:28 Comment(0)
F
1

I am gonna try to implement my own DifferenceListener to handle this need (see this post). Gonna post the result soon. It seems to work, gonna post the implementation tomorrow.

Fargone answered 9/3, 2011 at 17:3 Comment(2)
could you send example, please?Derina
You should take a look at this post (#1242093) it's pretty much like the listener I implemented.Fargone

© 2022 - 2024 — McMap. All rights reserved.