简体   繁体   English

有没有办法比较两个 xml 并且它应该忽略确切的内容文本但它应该按数据类型比较内容文本

[英]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:我已经尝试了下面的示例并尝试了DiffBuilderCompareMatcher类的不同方法:

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,它决定差异是SIMILAREQUAL还是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.

相关问题 如何通过值的数据类型比较两个 JSON 字符串,它应该忽略确切的值,它还应该忽略数组的长度,如下例所示 - How to compare two JSON String by data type of values, it should ignore exact values and it should also ignore length of array as per below example 比较 JAVA 中两个大文本文件内容的最快方法是什么 - What is the fastest way to compare the content of two BIG text files in JAVA 比较两个文本文件和Java拆分词的内容 - Compare content of two text files and split words java 如何比较两个文本文件的内容并在另一个文本文件中输出? 文字1-文字2 - How to compare the content of two text files and output in another text file? text1 - text2 比较两个列表的内容 - Compare content of two lists 如何比较两个文本文件的内容并返回“相同内容”或“不同内容”? - How to compare the contents of two text files and return “Same Content” or “Different Content”? 内容类型text / xml错误 - Wrong content type text/xml Jackson 不应忽略多根 xml 内容中的根元素标记 - Jackson should not ignore root element tag in multi root xml content 如何在Java中比较没有循环的2个文本文件(内容)? - How to compare 2 text files (content) without loop in Java? MTOM - 根内容类型始终为text / xml - MTOM - Root content type was always text/xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM