XMLUnit - Xml file indentation impacts on comparison

百般思念 提交于 2019-12-10 12:31:32

问题


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.

Here is the error message generated by XMLUnit:

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

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?

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.


回答1:


You can relax some of the constraints used by XMLUnit when comparing to trees by setting properties on the org.custommonkey.xmlunit.XMLUnit class.

In your case, you probably want:

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

You may also find the setIgnoredAttributeOrder property helpful as well.



来源:https://stackoverflow.com/questions/5248517/xmlunit-xml-file-indentation-impacts-on-comparison

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!