XMLUnit - Xml文件缩进会影响比较 [英] XMLUnit - Xml file indentation impacts on comparison

查看:509
本文介绍了XMLUnit - Xml文件缩进会影响比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用 XMLUnit 库来比较两个XML文件。
其中一个,候选,是由我的Java Objects代码(使用JAXB)生成的,另一个是引用(我无法修改它) 。
基本上我试图证明给定一个参考XML文件我可以反序列化它(使用Jaxb和我自己的一些类)然后将它序列化回另一个文件并且仍然具有相同的内容。

I am currently trying to use the XMLUnit library to compare two XML files. One of them, the candidate, is generated by my code from Java Objects (using JAXB) and the other one is the reference (I cannot modify it). Basically I am trying to prove that given a reference XML file I can unserialize it (using Jaxb and some classes of my own) then serialize it back to another file and still have the same content.

该库似乎提供了我需要的服务,但是当生成的文件没有正确缩进时(以漂亮的打印版本的形式),比较失败并且当缩进是好。
例如,当生成候选时没有缩进,内容是单行,如果正确(手动)缩进,则比较正常。

The library seems to furnish the services I need but when the generated file is not properly indented (in kind of a "pretty-print" version) the comparison fails and it doesn't when indentation is OK. For example when the candidate is generated there is no indentation, the content is a one-liner, if a indent it properly (manually) the comparison is OK.

以下是XMLUnit生成的错误消息:

Here is the error message generated by XMLUnit:


[不同]预期的孩子数
节点'3'但是'1'

[different] Expected number of child nodes '3' but was '1'

你们有什么想法解决这个问题吗?
也许解决方案是生成候选的漂亮版本,在这种情况下你有想法将它与JAXB序列化器结合起来吗?

Do you guys have any idea to solve this? Maybe the solution is to generate a pretty-print version of the candidate, in this case do you have an idea to combine it with the JAXB serialiser?

顺便说一句,如果你现在有一个更好的Java解决方案来比较XML文件我会很高兴知道它;)

By the way if you now a better solution in Java to compare XML files I'll be glad to know it ;)

提前谢谢为了你的帮助。

Thanks in advance for your help.

推荐答案

通过在<$上设置属性,可以放宽XMLUnit在与树比较时使用的一些约束。 c $ c> org.custommonkey.xmlunit.XMLUnit class。

在您的情况下,您可能需要:

In your case, you probably want:

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

您可能还会发现 setIgnoredAttributeOrder 属性有用同样。

You may also find the setIgnoredAttributeOrder property helpful as well.

这篇关于XMLUnit - Xml文件缩进会影响比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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