英文:
XMLunit compare only tags
问题
我想要比较两个XML文件,但仅基于标签的值进行比较,还需要将标签作为整体进行比较,例如:
<someTag><otherTag value1="0" value2="5"/>text1</someTag>
<someTag><otherTag value1="10" value2="15"/>text2</someTag>
我需要实现的目标是显示<otherTag value1="10" value2="15"/>
不同,我不关心text1
和text2
的差异。问题在于,默认情况下,XMLUnit会将value1
和value2
分别进行比较,而我希望将它们作为一个字符串进行比较。有没有办法实现这一点?我知道可以通过实现DifferenceEvaluator
来排除text
,但仍然不知道如何将属性作为整体进行比较。我还考虑使用XSLT。
英文:
I would like to compare two XML files, but based only on tag values, also I need to compare tags as a whole, e.g.
<someTag><otherTag value1="0" value2="5"/>text1</someTag>
<someTag><otherTag value1="10" value2="15"/>text2</someTag>
what I need to achieve is to show that <otherTag value1="10" value2="15"/>
is different, I don't care about text1
text2
difference. Problem is, that by default XMLUnit will compare value1
and value2
separetly, while I want to compare them together, whole tag as one string. Is there a way to achieve this? I know I can exclude text
implementing DifferenceEvaluator
but still, I don't know how to compare Attributes as whole. I also consider to use xslt.
答案1
得分: 0
我会将这两份文件进行转换,排除您希望从比较中排除的部分,然后对比结果。
英文:
I would transform both documents to get rid of the things you want to exclude from the comparison, and then compare the results.
答案2
得分: 0
我认为你可以将DifferenceEvaluators链接在一起来实现这一点。
例如,在我必须合并两个节点的比较时,我做过类似的事情。在你的情况下,可能需要进行一些定制。
Diff myDiff = DiffBuilder.compare(FileReadingUtil.readFileAsByteArray(source,this.getClass().getClassLoader()))
.withTest(FileReadingUtil.readFileAsByteArray(test,this.getClass().getClassLoader()))
.ignoreComments()
.ignoreWhitespace()
.withDifferenceEvaluator(
DifferenceEvaluators.chain(
DifferenceEvaluators.Default,
new IgnoreAttributeDifferenceEvaluator("MsgId"),
new IgnoreAttributeDifferenceEvaluator("PmtId")
)
)
.checkForSimilar()
.build();
英文:
I think you can chain the DifferenceEvaluators together to achieve this.
For example, this is something I did when I had to combine comparison for two nodes. Might work in your case with some customisations.
Diff myDiff = DiffBuilder.compare(FileReadingUtil.readFileAsByteArray(source,this.getClass().getClassLoader()))
.withTest(FileReadingUtil.readFileAsByteArray(test,this.getClass().getClassLoader()))
.ignoreComments()
.ignoreWhitespace()
.withDifferenceEvaluator(
DifferenceEvaluators.chain(
DifferenceEvaluators.Default,
new IgnoreAttributeDifferenceEvaluator("MsgId"),
new IgnoreAttributeDifferenceEvaluator("PmtId")
)
)
.checkForSimilar()
.build();
专注分享java语言的经验与见解,让所有开发者获益!
评论