使用XMLUnit将两个XML与不同数量的元素进行比较

时间:2016-01-02 17:49:31

标签: java xml xpath xmlunit

我有两个xmls,需要进行比较。两个xml中的元素数量可能会有所不同。比如说,考虑下面两个xmls:

XML1:

<Students>
                <Name Type="PG">
                    <GivenName>David</GivenName>
                    <Surname>John</Surname>
                </Name>
                <Name Type="PG">
                    <GivenName>Naveen</GivenName>
                    <Surname>S</Surname>
                </Name>
                <Telephone AreaCityCode="BLR">
                    <PhoneNumber No="+91-9643542222"/>
                </Telephone>
               <Telephone AreaCityCode="BLR">
                    <PhoneNumber No="+91-9643542258"/>
                </Telephone>
                <Email>David.JOHN@gmail.com</Email>
                <Email>Naveen.S@gmail.com</Email>
                <Address Type="PT">
                    <AddressLine>40 DICKENSON ROAD</AddressLine>
                    <AddressLine>BANGALORE 12</AddressLine>
                </Address>
                <Address Type="TP">
                    <AddressLine>DOUBLE ROAD</AddressLine>
                    <AddressLine>BANGALORE 20</AddressLine>
                </Address>
</Students>

XML2:

 <Students>
                <Name Type="PG">
                    <GivenName>David</GivenName>
                    <Surname>John</Surname>
                </Name>
                <Name Type="PG">
                    <GivenName>Naveen</GivenName>
                    <Surname>S</Surname>
                </Name>
                <Telephone AreaCityCode="BLR">
                    <PhoneNumber No="+91-9643542222"/>
                </Telephone>
               <Telephone AreaCityCode="BLR">
                    <PhoneNumber No="+91-9643542258"/>
                </Telephone>
                <Email>David.JOHN@gmail.com</Email>
                <Address Type="PT">
                    <AddressLine>40 DICKENSON ROAD</AddressLine>
                    <AddressLine>BANGALORE 12</AddressLine>
                </Address>
                <Address Type="TP">
                    <AddressLine>DOUBLE ROAD</AddressLine>
                    <AddressLine>BANGALORE 30</AddressLine>
                </Address>
</Students>

在XML2中,email元素的数量只有一个,但在XML1中它是两个。我的任务是比较AddressLine元素的内容,而不管email个元素的数量。我已经写了一段代码来获得这个,如下所示:

XMLUnit.setIgnoreWhitespace(true);
XMLUnit.setIgnoreAttributeOrder(true);
XMLUnit.setIgnoreComments(true);
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true); 
 DetailedDiff detailledDiff = new DetailedDiff (XMLUnit.compareXML(XML1, XML2));
    detailledDiff.overrideElementQualifier(new ElementNameAndTextQualifier());
    List<Difference> list = detailledDiff.getAllDifferences();
    for (Difference difference : list) {
        if(difference.getTestNodeDetail().getXpathLocation() == null)
            System.out.println("control: "+difference.getControlNodeDetail().getXpathLocation());
        if(difference.getControlNodeDetail().getXpathLocation() == null)
            System.out.println("test: "+difference.getTestNodeDetail().getXpathLocation());
    }

即使AddressLine发生了变化,它也不会打印addressLine XPath。请帮我解决这个问题。提前致谢

1 个答案:

答案 0 :(得分:0)

我想也许你的某个文件中有太多空格(空格,换行符等)。要忽略空格,请在测试人员的开头添加以下行:

    XMLUnit.setIgnoreWhitespace(true);