我第一次使用 xmlunit 来比较 2 个 xml。它显示出巨大的希望,但在第一个障碍上就失败了。它正在比较两个几乎相同的 xml 片段并声称它们是不同的。
Diff diff = new Diff(control, test);
diff.overrideDifferenceListener(new IgnoreTextAndAttributeValuesDifferenceListener());
xmlunit返回的结果如下:
[different] Expected number of child nodes '3' but was '2' - comparing <SOAP-ENV:Envelope...> at /Envelope[1] to <SOAP-ENV:Envelope...> at /Envelope[1]
但是 xml 几乎是一样的。这是控件:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope">
<SOAP-ENV:Body>
<v:messagegroup xmlns:v="http://www.outfit.net/chargingandpayments/message/1.0">
<v:request>
<v:msgcontrol>
<v:country>GB</v:country>
<v:caller>
<v:name>CORE</v:name>
<v:signature>Signature</v:signature>
<v:version>v10</v:version>
</v:caller>
<v:headers/>
</v:msgcontrol>
<v:validate>
<v:accountId>MSISDN</v:accountId>
</v:validate>
</v:request>
</v:messagegroup>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是测试字符串:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope">
<SOAP-ENV:Body>
<v:messagegroup xmlns:v="http://www.outfit.net/chargingandpayments/message/1.0">
<v:request>
<v:msgcontrol>
<v:country>GB</v:country>
<v:caller>
<v:name>CORE</v:name>
<v:signature>Signature</v:signature>
<v:version>v10</v:version>
</v:caller>
<v:headers />
</v:msgcontrol>
<v:validate>
<v:accountId>lblabla</v:accountId>
</v:validate>
</v:request>
</v:messagegroup>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我做错了什么?
最佳答案
区别在于 SOAP-ENV:Envelope
的子级数量,其中 XMLUnit 看到两个或三个子级。我只看到一个“真实”子级,因此其余部分可能由元素内容空白组成。
XMLUnit.setIgnoreWhitespace(true);
在评估差异之前应该解决这个问题。
关于java - xmlunit 在相同的 xml 中发现差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23387582/