[英]Is there any way to compare two xml and it should ignore exact content text but it should compare content text by Data type
I have tried the below example and tried with different methods of DiffBuilder
and CompareMatcher
classes:我已经尝试了下面的示例并尝试了
DiffBuilder
和CompareMatcher
类的不同方法:
import org.junit.jupiter.api.Test;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.ElementSelectors;
import org.xmlunit.matchers.CompareMatcher;
import static org.hamcrest.MatcherAssert.assertThat;
public class XmlDemo4 {
@Test
public void demoMethod() {
String actual = "<struct><int>3</int><boolean>false</boolean></struct>";
String expected = "<struct><boolean>false</boolean><int>4</int></struct>";
assertThat(actual, CompareMatcher.isSimilarTo(expected)
.ignoreWhitespace().normalizeWhitespace().
withNodeMatcher(new
DefaultNodeMatcher(ElementSelectors.byName,ElementSelectors.Default)));
}
}
by running above code I am getting:通过运行上面的代码我得到:
java.lang.AssertionError:
Expected: Expected text value '4' but was '3' - comparing <int ...>4</int> at
/struct[1]/int[1]/text()[1] to <int ...>3</int> at /struct[1]/int[1]/text()[1]:
<int>4</int>
but: result was:
<int>3</int>
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at StringXml.XmlDemo4.demoMethod(XmlDemo4.java:29)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
Process finished with exit code -1
here it is comparing content text also, please suggest, is there any way to compare the content text with the data type and it should not compare with the exact content text这里也在比较内容文本,请建议,有什么方法可以将内容文本与数据类型进行比较,而不应该与确切的内容文本进行比较
You need some implementation of XML diff.您需要一些 XML diff 的实现。 I am not aware of Java, but there is some python library: https://pypi.org/project/xmldiff/
我不知道 Java,但有一些 python 库: https://pypi.org/project/xmldiff/
See also Are there any free Xml Diff/Merge tools available?另请参阅是否有任何免费的 Xml 差异/合并工具可用?
DifferenceEvaluator
is the class which decides whether a difference is SIMILAR
, EQUAL
or DIFFERENT
. DifferenceEvaluator
是 class,它决定差异是SIMILAR
、 EQUAL
还是DIFFERENT
。 In my approach I am saying if there is any comparison of type TEXT
then make the result of those comparison as SIMILAR
Refer to XMLUnit 2.x在我的方法中,我是说如果有任何
TEXT
类型的比较,那么将这些比较的结果作为SIMILAR
参考 XMLUnit 2.x
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.diff.*;
import static org.xmlunit.diff.ComparisonType.TEXT_VALUE;
public class XmlDemo {
public static void main(String[] args) {
String actual = "<struct><int>6</int><boolean>false</boolean></struct>";
String expected = "<struct><boolean>false</boolean><int>4</int></struct>";
Diff diff = DiffBuilder.compare(actual)
.checkForSimilar()
.withTest(expected)
.ignoreWhitespace().normalizeWhitespace()
.withNodeMatcher(new
DefaultNodeMatcher(ElementSelectors.byName))
.withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, DifferenceEvaluators.downgradeDifferencesToSimilar(TEXT_VALUE)))
.build();
for (Difference difference: diff.getDifferences())
System.out.println(difference);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.