比较嵌套的XML节点并忽略所有其他内容?

时间:2019-03-07 21:15:43

标签: java xml xmlunit xmlunit-2

是否可以比较xml的各个部分并忽略其余部分?

例如:

<person>
    <addr>want to ignore</addr>
    <phone>
        <home>5555</home>
        <mobile>9090</mobile>
        <work>
            <email>a@a.com</email>
            <mobile>9090</mobile>
            <home>12121</home>
        </work>
    </phone>
    <hobby>want to ignore</hobby>
</person>

我只想比较“工作”元素及其子元素,而忽略其余部分,请记住房屋和移动设备的顺序可能不同。我不感兴趣的其他值也可能有所不同。

        // When // Then
        ElementSelector ignoreSelector = ElementSelectors
                .conditionalBuilder()
                .whenElementIsNamed("phone")
                .thenUse(ElementSelectors.byXPath(".//work", ElementSelectors.byNameAndText))
                .elseUse(ElementSelectors.conditionalSelector(it -> false, byName)).build();

        assertThat(actualXML,
                isSimilarTo(controlXML)
                        .ignoreWhitespace()
                        .withNodeMatcher(new DefaultNodeMatcher(ignoreSelector)));

我得到: java.lang.AssertionError: 预期:预期孩子为“人”,但为“空”-/ person [1]与:

进行比较

0 个答案:

没有答案