比较 Groovy/Java 中的两个 XML 字符串/文件 [英] Comparing two XML strings/files in Groovy/Java

查看:22
本文介绍了比较 Groovy/Java 中的两个 XML 字符串/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写单元测试来检查一些 XML 构建器.

I'm writing unit tests for checking some XML builder.

现在我遇到了预期结果和实际结果之间语法差异的问题,尽管它们的语义相同.

Now I'm running into the problem of syntactical differences between the expected result and the actual result, despite their identical semantics.

示例:

预期结果:

<parent><child attr="test attribute">text here</child></parent>

实际结果:

<parent>
  <child attr="test attribute">
    text here
  </child>
</parent>

我尝试使用 XmlUtil.serialize() 规范化 xml,但这似乎保留了空格,留下了语法差异.

I tried normalizing the xml using XmlUtil.serialize(), however this seems to keep the whitespaces, leaving syntactical differences.

如何获得 xml 字符串的规范化/规范形式以使我的测试更健壮?

How can I get the normalized/canonical form of xml strings in order to make my tests more robust?

我正在编写 Grails 应用程序,因此我可以使用 Groovy 或 Java 中的任何解决方案.

I'm writing a Grails application, so I'm fine with any solution in Groovy or Java.

推荐答案

您可以像这样使用 Groovy XMLUnit 实用程序:

You can use the Groovy XMLUnit utility like this:

XMLUnit.setIgnoreWhitespace(true)
XMLUnit.setIgnoreComments(true)
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true)
XMLUnit.setNormalizeWhitespace(true)

XMLUnit.compareXML(expectedXml, actualXml)

在忽略语法差异的情况下比较 XML 文件.

To compare XML files while ignoring the syntactical differences.

这篇关于比较 Groovy/Java 中的两个 XML 字符串/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆