简体   繁体   English

Java 比较 XML 不同名称的标签

[英]Java compare XML tags with different names

I have two XML's which have basically the same content but a single tag has a different name:我有两个 XML 的内容基本相同,但一个标签的名称不同:

actualSource:实际来源:

<ns2:Message xmlns:ns2="http://blabla.blabla.com">
    <data>
        <fieldabc>aaaa</fieldabc>
    </data>
    <asset>
        <field1>50.85</field1>
        <field2>Scooter</field2>
        <field3>Small</field3>
    </asset>

expectedSource:预期来源:

<ns2:Message xmlns:ns2="http://different.blabla.com">
    <data>
        <fieldabc>aaaa</fieldabc>
    </data>
    <product>
        <field1>50.85</field1>
        <field2>Scooter</field2>
        <field3>Small</field3>
    </product>
</ns2:Message>

The 'asset' and 'product' tags have same content, same fields name. “资产”和“产品”标签具有相同的内容、相同的字段名称。 I am using xmlunit:我正在使用 xmlunit:

    <dependency>
        <groupId>org.xmlunit</groupId>
        <artifactId>xmlunit-core</artifactId>
        <version>2.7.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.xmlunit</groupId>
        <artifactId>xmlunit-matchers</artifactId>
        <version>2.7.0</version>
        <scope>test</scope>
    </dependency>

I am trying to compare the via DiffBuilder:我正在尝试比较通过 DiffBuilder:

Diff diff = DiffBuilder.compare(expectedSource).withTest(actualSource)
                .ignoreWhitespace()
                .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText))
                .checkForSimilar()
                .build()

In differences I receive:在我收到的差异中:

Expected child 'null' but was '{http://blabla.blabla.com}Message' - comparing <NULL> to <ns2:Message...> at /Message[1] (DIFFERENT)
Expected child '{http://different.blabla.com}Message' but was 'null' - comparing <ns2:Message...> at /Message[1] to <NULL> (DIFFERENT)

I need to compare those 2 messages if they are equal without unmarshalling them to java objects, I need to assert pure XML's.如果它们相等,我需要比较这两条消息而不将它们解组为 java 对象,我需要断言纯 XML。

ElementSelectors.byNameAndText has not considered your two Message elements as candidates for comparison as it is sensitive to XML namespace URIs. ElementSelectors.byNameAndText没有将您的两个Message元素视为比较的候选对象,因为它对 XML 命名空间 URI 很敏感。 This is true for all built-in ElementSelector s.这适用于所有内置的ElementSelector From an XML perspective the two elements are completely different things.从 XML 的角度来看,这两个元素是完全不同的东西。

You need to provide an ElementSelector implementation that only looks at the local name of your elements.您需要提供一个ElementSelector实现,它只查看元素的本地名称。 Something like就像是

    @Override
    public boolean canBeCompared(final Element controlElement,
                                 final Element testElement) {
        if (controlElement == null || testElement == null) {
            return false;
        }
        final String controlLocal = controlElement.getLocalName();
        final String controlName = controlLocal == null ? controlElement.getNodeName() : controlLocal;
        final String testLocal = testElement.getLocalName();
        final String testName = testLocal == null ? testElement.getNodeName() : testLocal;
        return controlName.equals(testName);
    }

would be a namespace ignoring version of byName .将是一个名称空间忽略版本的byName

Using this will not be enough, though, as you'll get differences of type ComparisonType.NAMESPACE_URI which will make your documents DIFFERENT .但是,使用它是不够的,因为您会得到ComparisonType.NAMESPACE_URI的差异,这将使您的文档DIFFERENT In order to get rid of them you must also override the DifferenceEvaluator .为了摆脱它们,您还必须覆盖DifferenceEvaluator The easiest solution is to use DifferenceEvaluators.downgradeDifferencesToSimilar(ComparisonType.NAMESPACE_URI) .最简单的解决方案是使用DifferenceEvaluators.downgradeDifferencesToSimilar(ComparisonType.NAMESPACE_URI)

With a combination like this you basically remove namespace sensitivity from XMLUnit's difference engine.通过这样的组合,您基本上可以从 XMLUnit 的差异引擎中删除名称空间敏感性。

Personally I'd recommend fixing the cause of the difference in namespace URIs, though.不过,我个人建议修复命名空间 URI 差异的原因。 I'm afraid it is going to byte you sooner or later as namespace URIs are all but irrelevant to a lot of XML processing tools.恐怕它迟早会让你感到厌烦,因为命名空间 URI 与许多 XML 处理工具几乎无关。 But you will certainly know your case a lot better than I do.但你肯定会比我更了解你的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM