【问题标题】:Finding differences in two XML files using XMLUnit使用 XMLUnit 查找两个 XML 文件中的差异
【发布时间】:2012-06-12 13:58:20
【问题描述】:

如何在使用 XMLUnit 的DetailedDiff 函数时只检查特定节点而不是所有节点的差异

这是我的代码:

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);
    }

但问题是,我只想让程序告诉我 4 个特定节点是否发生了任何变化。那么如何通过列出所有差异来实现这一目标。

【问题讨论】:

    标签: java xmlunit


    【解决方案1】:

    您可以实现自己的DifferenceListener,忽略某些节点(或只关注某些节点)并告诉 XMLUnit 在检查差异时使用它。这是一个忽略任何名为“myelement”的元素的DifferenceListener:

    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
          }
    }
    

    然后将其传递给DetailedDiff:

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

    【讨论】:

      【解决方案2】:

      您可以先使用javax.xml.transform.TransformerFactory 转换两个文档,以便它只包含您感兴趣的节点。或者您可以在差异上使用 getControlNodeDetail() 和 getTestNodeDetail() 来查看差异是否适用于您想要的节点。

      【讨论】:

        猜你喜欢
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 2014-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多