xml - 常规 : Compare SOAP Response with XML file

标签 xml groovy compare xmlunit xmlunit-2

我想在 groovy 代码中比较我的 Soap Response 和忽略顺序的 xml 文件:

这是我的代码:

import org.custommonkey.xmlunit.Stuff
import org.xmlunit.Stuff

//ExpectedString is my xml converted to text, same for ResponseString

Diff diff = DiffBuilder.compare(ExpectedString)
           .withTest(ResponseString)
           .ignoreComments()
           .ignoreWhitespace()
           .checkForSimilar()
           .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
           .build();

assertFalse("XML similar " + diff.toString(), diff.hasDifferences())

所以,正如您所看到的,我使用了 DefaultNodeMatcher,我使用了 XMLUnit2.0 ...没有结果(甚至没有忽略顺序或在比较时出现异常错误)

有解决办法吗?解决这个问题

因为我迫切希望找到一个直接的,我可以对我的 xml 和我的 soap 响应进行排序,以便我可以有一个简单的差异吗?有没有办法按字母顺序逐行排序?如果是,怎么办?

谢谢大家!

更新:

这是我简化的 XML 结构

<body>
<stuff>
  <miniStuff></miniStuff>
  <miniStuff></miniStuff>
</stuff>
<Services>
  <Service>
    <tag1>ValueA</tag1>
    <tag2>ValueAA</tag2>
  </Service>
  <Service>
    <tag1>ValueB</tag1>
    <tag2>ValueBB</tag2>
  </Service>
</services>
</body>

我的问题是我不能保证 ValueA 是第一个而不是第二个

最佳答案

这是您可能正在寻找的那个:使用 ByNameAndTextRecSelector

withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))

单元测试:

    @Test
    public void testDiffOrder() {
        final String control = """
            <r>
                <ser>
                    <t1>a</t1>
                    <t2>b</t2>
                </ser>
                <ser>
                    <t1>d</t1>
                    <t2>e</t2>
                </ser>
            </r>"""
        final String test = """
            <r>
                <ser>
                    <t1>d</t1>
                    <t2>e</t2>
                </ser>
                <ser>
                    <t1>a</t1>
                    <t2>b</t2>
                </ser>
            </r>"""
        Diff diff = DiffBuilder.compare(Input.fromString(control))
                .withTest(Input.fromString(test))
                .ignoreComments()
                .ignoreWhitespace()
                .checkForSimilar()
                .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))
                .build()

        assertFalse("XML differ " + diff.toString(), diff.hasDifferences())
    }

感谢@bodewig。 样本来自 here

更新:更多 groovified 版本带有 OP 原始 xml 片段。

import org.xmlunit.builder.DiffBuilder
import org.xmlunit.builder.Input
import org.xmlunit.diff.ByNameAndTextRecSelector
import org.xmlunit.diff.DefaultNodeMatcher
import org.xmlunit.diff.ElementSelectors

def control = """<body>
   <stuff>
      <miniStuff />
      <miniStuff />
   </stuff>
   <Services>
      <Service>
         <tag1>ValueB</tag1>
         <tag2>ValueBB</tag2>
      </Service>
      <Service>
         <tag1>ValueA</tag1>
         <tag2>ValueAA</tag2>
      </Service>
   </Services>
</body>"""
def test = """<body>
   <stuff>
      <miniStuff />
      <miniStuff />
   </stuff>
   <Services>
      <Service>
         <tag1>ValueA</tag1>
         <tag2>ValueAA</tag2>
      </Service>
      <Service>
         <tag1>ValueB</tag1>
         <tag2>ValueBB</tag2>
      </Service>
   </Services>
</body>"""
def myDiff = DiffBuilder.compare(Input.fromString(control))
            .withTest(Input.fromString(test))
            .checkForSimilar()
            .withNodeMatcher(new DefaultNodeMatcher(new ByNameAndTextRecSelector(),ElementSelectors.byName))
            .build()
println myDiff.toString()
println myDiff.hasDifferences()
assert !myDiff.hasDifferences()

关于xml - 常规 : Compare SOAP Response with XML file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40743664/

相关文章:

android - 比较两个数组列表元素并获取不常见的元素

list - 比较Python中两个列表中的两个单词

c# - 将数据从 XML 读取到数组中

xml - DOM4J:检索按属性值过滤的节点值

sql-server - SQL Azure 是否支持 'FOR XML'

grails - 迭代可能是字符串或字符串数​​组的 Groovy 对象

scripting - 从脚本数据源访问 Pentaho 中的参数?

android - 单击深层链接 url 打开带有红色边框的应用程序

gradle - 理解 Gradle-Groovy 语义

swift - 比较两个枚举变量而不考虑它们的关联值