我正在编写一个比较工具,它可以让我在不考虑顺序的情况下比较两个 xml 的相似性。我正在使用 xmlunit 2.4.0
org.xmlunit.diff.Diff diff = DiffBuilder.compare(xml1)
.withTest(xml2)
.checkForSimilar()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
.build();
因此,以下两个 xmls 比较成功
xml1:
<multistatus>
<flowers>
<flower>Roses</flower>
<flower>Daisy</flower>
</flowers>
<flowers>
<flower>Roses</flower>
<flower>Daisy</flower>
</flowers>
</multistatus>
xml2:
<multistatus>
<flowers>
<flower>Roses</flower>
<flower>Daisy</flower>
</flowers>
<flowers>
<flower>Daisy</flower>
<flower>Roses</flower>
</flowers>
</multistatus>
但是,当我给出以下输入时,这会失败:xml1:
<multistatus>
<flowers>
<flower>Roses</flower>
</flowers>
<flowers>
<flower>Daisy</flower>
</flowers>
</multistatus>
xml2:
<multistatus>
<flowers>
<flower>Daisy</flower>
</flowers>
<flowers>
<flower>Roses</flower>
</flowers>
</multistatus>
我尝试创建 ElementSelector ,即使这样也无济于事。
ElementSelector selector = ElementSelectors.conditionalBuilder()
.whenElementIsNamed("flowers").thenUse(ElementSelectors.byXPath("./flowers/flower", ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byName)
.build();
org.xmlunit.diff.Diff diff = DiffBuilder.compare(refSource)
.withTest(testSource)
.checkForSimilar()
.ignoreWhitespace()
.normalizeWhitespace()
.withNodeMatcher(
new DefaultNodeMatcher(
selector,ElementSelectors.Default)
)
.build();