Finding differences in two XML files using XMLUnit

回眸只為那壹抹淺笑 提交于 2019-12-11 07:15:32

问题


How to check for only particular nodes and not all nodes for difference while using the DetailedDiff function of XMLUnit

this is my code:

public static void main(String[] args) 
    {
        //URL url1 = xmlunittest.class.getResource("MSHIS1.xml");
    //  URL url2 = xmlunittest.class.getResource("MSHIS.xml");

        Logger L = new Logger();

        FileReader fr1 = null;
        int countofdifferences = 0;
        FileReader fr2 = null;
        try {
            fr1 = new FileReader("MSHIS.xml");
            fr2 = new FileReader("MSHIS1.xml");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

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


            DetailedDiff detDiff = new DetailedDiff(diff);
            List differences = detDiff.getAllDifferences();
            for (Object object : differences) {
                Difference difference = (Difference)object;
                L.writeToBVTLOG("------------------------");
                L.writeToBVTLOG(difference.toString());
                L.writeToBVTLOG("------------------------");

                countofdifferences++;
            }

        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        L.writeToBVTLOG(countofdifferences);
    }

But the thing is that, I just want the program to tell me if 4 particular nodes have undergone any changes. So how to get there from listing all the differences.


回答1:


You might transform both documents first with javax.xml.transform.TransformerFactory so that it only contains the nodes you are interested. Or you could use getControlNodeDetail() and getTestNodeDetail() on Difference to see if the difference applies to the nodes you want.




回答2:


You can implement your own DifferenceListener that ignores some nodes (or only pays attention to some nodes) and tell XMLUnit to use it when checking differences. Here's a DifferenceListener that ignores any elements named "myelement":

public class IgnoreMyAttributeElements implements DifferenceListener()
{

      @Override
      public int differenceFound(Difference differenceIn)
      {
         Node controlNode = differenceIn.getControlNodeDetail().getNode();

         if(controlNode.getNodeType() == Node.ELEMENT_NODE)
         {
            if(controlNode.getLocalName().equals("myelement");
            {
               return DifferenceListener.RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
            }
         }

         return DifferenceListener.RETURN_ACCEPT_DIFFERENCE;
      }

      @Override
      public void skippedComparison(Node nodeIn, Node nodeIn2)
      {
         //don't care about these events
      }
}

Then pass it to the DetailedDiff:

DetailedDiff diff =  new DetailedDiff(XMLUnit.compareXML(oldXml, newXml));
diff.overrideDifferenceListener(new IgnoreMyAttributeElements());
List<?> allDifferences = diff.getAllDifferences();
//do something with differences


来源:https://stackoverflow.com/questions/10998229/finding-differences-in-two-xml-files-using-xmlunit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!