2

I have 2 xml files with almost similar structure, In these XML files how to fetch the following informations ??

  1. Controls (or Nodes) newly added
  2. Controls (or Nodes) removed and
  3. Control (or Nodes) properties modified.

For your infomration, I tried the following code :

    File f1 = new File(inputXMLPath);
    File f2= new File(outputXMLPath);
    FileReader fr1 = null;
    FileReader fr2 = null;
    try {
        fr1 = new FileReader(f1);
        fr2 = new FileReader(f2);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    try {
        Diff diff = new Diff(fr1, fr2);
        System.out.println("Similar? " + diff.similar());
        System.out.println("Identical? " + diff.identical());

        DetailedDiff detDiff = new DetailedDiff(diff);

        detDiff.overrideDifferenceListener(new DifferenceListener() {
            @Override
            public int differenceFound(Difference diff) {
                if (diff.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
                    || diff.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID) {
                    return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
                }
                return RETURN_ACCEPT_DIFFERENCE;
            }
            @Override
            public void skippedComparison(Node arg0, Node arg1) { }

        });


        List differences = detDiff.getAllDifferences();
        for (Object object : differences) {
            Difference difference = (Difference)object;
            System.out.println("***********************");
            System.out.println(difference);
            System.out.println("***********************");
        }

    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
Ragu
  • 187
  • 1
  • 11
  • check their [documentation](http://xmlunit.sourceforge.net/api/) – Nitesh Virani Aug 07 '15 at 11:38
  • Controls newly added\removed informations are w.r.t the first xml file. – Ragu Aug 07 '15 at 11:39
  • It always compares the contents blindly and the code (or the XMLUnit API) which I wrote doesn't do a brilliant comparison like Nodes newly added\removed etc.., Can you pls assist me in acheiving this. Suggestions are greatly appreciated !! – Ragu Aug 07 '15 at 11:55
  • @SuperBiasedMan: I've posted the code.. Can you give me a suggestion on how to acheive this ?? – Ragu Aug 07 '15 at 12:12

1 Answers1

1

[EDIT: added note about setCompareUnmatched and example code]

I think you want to react to CHILD_NODE_NOT_FOUND Differences that XMLUnit 1.x will emit if you use XMLUnit.setCompareUnmatched(false).

If the control node of the Difference is null, the node was added - if the test node is null it was removed. Any other difference is a change to a Node that XMLUnit thinks is present on either side.

For example

    FileReader fr1 = new FileReader(f1);
    FileReader fr2 = new FileReader(f2);
    XMLUnit.setCompareUnmatched(false);
    Diff diff = new Diff(fr1, fr2);
    System.out.println("Similar? " + diff.similar());
    System.out.println("Identical? " + diff.identical());

    DetailedDiff detDiff = new DetailedDiff(diff);

    detDiff.overrideDifferenceListener(new DifferenceListener() {
        @Override
        public int differenceFound(Difference diff) {
            if (diff.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
                || diff.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID) {
                return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
            return RETURN_ACCEPT_DIFFERENCE;
        }
        @Override
        public void skippedComparison(Node arg0, Node arg1) { }

    });


    List differences = detDiff.getAllDifferences();
    for (Object object : differences) {
        Difference difference = (Difference)object;
        System.out.println("***********************");
        if (difference.getId() == DifferenceConstants.CHILD_NODE_NOT_FOUND_ID) {
            if (difference.getControlNodeDetail().getNode() == null) {
                System.out.println("Node was added");
            } else {
                System.out.println("Node was removed");
            }
        }
        System.out.println("***********************");
    }

will lead to something like

***********************
Expected presence of child node 'baz' but was 'null' - comparing <baz...> at /foo[1]/baz[1] to  at null
Node was removed
***********************
***********************
Expected presence of child node 'null' but was 'xyzzy' - comparing  at null to <xyzzy...> at /foo[1]/xyzzy[1]
Node was added
***********************
Stefan Bodewig
  • 3,260
  • 15
  • 22
  • Thanks for your answer.. Can you illustrate with the help of any sample code. So that I can still understand better. – Ragu Aug 07 '15 at 14:50
  • Done. One thing I forgot to explicitly mention, Ragu, is the `setCompareUnmatched` method that you need to us to get around XMLUnit 1's default behavior. – Stefan Bodewig Aug 09 '15 at 05:29
  • Thanks for you answer. After pasting your code . I'm facing some problems during comparsions. The node added\removed information(s) seems to be incorrect. – Ragu Aug 10 '15 at 10:29
  • I've uploaded the 2 xml files (that I've used for comparisons) in the below link :[link] (http://www.4shared.com/account/home.jsp#dir=TRoc4FW6) and you can see that the result (Nodes that are been 'Added\Removed' ) which the XMLUnit displays seems to be incorrect. – Ragu Aug 10 '15 at 10:33
  • Stefan : Do I need to do any other thing (like implementing any Interface..) to resolve this kind of issues during comparison Process. – Ragu Aug 10 '15 at 12:16
  • Ragu, I'm unable to access the files without registering with 4share. The most likely reason for "wrong" results is that XMLUnit picks the wrong elements to compare - in which case you'd need to look at the `ElementQualifier`. – Stefan Bodewig Aug 11 '15 at 03:54
  • Stefan, I tried by overriding the Element Qualifier assuming it be the reason for the Un-expected behavior. like below : detDiff.overrideElementQualifier(new ElementNameAndAttributeQualifier() ); and now the newly added\removed nodes are getting displayed correctly – Ragu Aug 11 '15 at 12:28
  • Stefan, But I do face another issue now, The issue is that when I only have the attribute value as a difference,. Now it is considering this also as a newly added\removed nodes. This is in-correct. Any suggestion please.. I'm totally confused :( the way the XMLUnit works. – Ragu Aug 11 '15 at 12:40
  • I'm not sure comments to this issue are the best place to discuss this. The xmlunit list? Whether a changed attribute results in no matches really depends on the `ElementQualifier` you've chosen, see the user guide for help on how XMLUnit selects the nodes it matches. http://xmlunit.sourceforge.net/userguide/html/index.html – Stefan Bodewig Aug 11 '15 at 17:54
  • Stefan : ElementNameQualifier did the trick for me. Overriding the overrideElementQualifier to ElementNameQualifier resolves the issue, The Reason why ElementNameAndAttributeQualifier() has this issue is this Class compares the attributes values for equality (in addition to the list of attributes) and so if any Node's attribute value is changed then the "areAttributesComparable()" method returns false, So the fix to this can be, either we can extend this Qualifier and have our own implementation for areAttributesComparable() method or we can use the Base ElementNameQualifier itself. – Ragu Aug 12 '15 at 12:01
  • Stefan, Thanks for your support !! :) :) – Ragu Aug 12 '15 at 12:04
  • And at last, If difference ID is an attribute change ID (i.e.., DifferenceConstants.ATTR_VALUE_ID) how to retrieve its owner node information ?? Currently AttrNSimple Instance contains the ownernode information but I coudn't access this Member Variable. Any ways of fetching this one ?? – Ragu Aug 12 '15 at 12:08
  • The `Node` is an `Attr` in this case which has got a `getOwnerElement` method http://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Attr.html – Stefan Bodewig Aug 13 '15 at 03:58
  • Stefan : Yes, I got it. I was able to obtain the Owner element from the Attr class. Really Thanks a lot for your help :) :) :) – Ragu Aug 13 '15 at 10:50