【问题标题】:XmlUnit and Child NodesXmlUnit 和子节点
【发布时间】:2013-05-22 02:24:47
【问题描述】:

我正在使用 xmlunit 来比较两个文本文件。控件xml为:

<books>
    <book>
       <name>Angels &amp; Demons</name>
       <isbn>9971-5-0210-0</isbn>
       <author>Dan Brown</author>
       <category></category>
    </book>
</books>

我将它与另一个交换了 and 元素的 xml 进行比较。

<books>
    <book>
        <isbn>9971-5-0210-0</isbn>
        <name>Angels &amp; Demons</name>
        <author>Dan Brown</author>
        <category></category>
    </book>
</books>

Diff 对象报告以下差异:

Expected sequence of child nodes '1' but was '3' - comparing <name...> at /books[1]/book[1]/name[1] to <name...> at /books[1]/book[1]/name[1]

如果&lt;name&gt; 是子节点“1”,那么&lt;isbn&gt; 不是子节点“2”吗?

【问题讨论】:

  • 似乎 XmlUnit 将 xml 中的回车计算为子节点。设置以下 XMLUnit.setIgnoreWhitespace(true); 给出了更直观的Expected sequence of child nodes '0' but was '1' - comparing &lt;int...&gt; at /struct[1]/int[1] to &lt;int...&gt; at /struct[1]/int[1] 结果

标签: xml xmlunit


【解决方案1】:

对于 xmlUnit 2.X 版本:

对于 xmlUnit 2.xx 版本,XMLUnit.setIgnoreWhitespace(true) 不再适用。现在,您可以通过添加 DiffBuilder.ignoreWhitespace() 将“忽略空格”直接添加到 DiffBuilder。

Diff diffXml =DiffBuilder.compare(expectedXml).withTest(actualXml).normalizeWhitespace().checkForSimilar().build();

为了断言 xmls 是相似的,你可以例如。做:

   MatcherAssert.assertThat(diffXml.toString(), is("[identical]"));

有关 1.x 到 2.x 之间更改的更多信息,请参阅: https://github.com/xmlunit/user-guide/wiki/Migrating-from-XMLUnit-1.x-to-2.x

【讨论】:

    【解决方案2】:

    似乎 XmlUnit 将 xml 中的回车计数为子节点。设置以下 XMLUnit.setIgnoreWhitespace(true);给出了更直观的子节点序列“0”但为“1”的预期结果 - 比较&lt;int...&gt; at /struct[1]/int[1] to &lt;int...&gt; at /struct[1]/int[1]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 1970-01-01
      相关资源
      最近更新 更多