如何在 XMLUnit 中比较两个相似的 XML 文件

发布于 2024-08-11 12:22:28 字数 282 浏览 3 评论 0原文

我想使用 XMLUnit 来比较两个相似的 XML 文件。

基本上每一件事都是相同的, File1File2 的副本,但在 File2 中我更改了一个节点中某些元素的顺序。

我正在尝试运行一个测试,比较这些文件并返回相似的结果,而不是将这些文件视为不同

I want to use XMLUnit to compare two similar XML files.

Basically every thing is same, File1 is a copy of File2 , but in File2 I have changed the order of some elements in one node.

I am trying to run a test where it compares these files and returns a result of similar and not treat these files as different.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷乱花海 2024-08-18 12:22:29

我认为此链接可以帮助您 - http://www.ibm .com/developerworks/java/library/j-cq121906.html#N10158

基本上,如果您的 File1 类似于 -

<account>
 <id>3A-00</id>
 <name>acme</name>
</account>

并且 File2 相同,但仅 的顺序不同 -

<account>
 <name>acme</name>
 <id>3A-00</id>
</account> 

然后您可以编写如下所示的测试,该测试将比较这些并返回类似的结果。

public void testIdenticalAndSimilar() throws Exception {
   String controlXML = "<account><id>3A-00</id><name>acme</name></account>";
   String testXML = "<account><name>acme</name><id>3A-00</id></account>"; 
   Diff diff = new Diff(controlXML, testXML);
   assertTrue(diff.similar());
   assertFalse(diff.identical());
}

希望有帮助。

I think this link can help you - http://www.ibm.com/developerworks/java/library/j-cq121906.html#N10158

Basically, if your File1 is like -

<account>
 <id>3A-00</id>
 <name>acme</name>
</account>

And File2 is same, but only differs in order of <name> and <id> -

<account>
 <name>acme</name>
 <id>3A-00</id>
</account> 

Then you can write a test like below which will compare these and return similar.

public void testIdenticalAndSimilar() throws Exception {
   String controlXML = "<account><id>3A-00</id><name>acme</name></account>";
   String testXML = "<account><name>acme</name><id>3A-00</id></account>"; 
   Diff diff = new Diff(controlXML, testXML);
   assertTrue(diff.similar());
   assertFalse(diff.identical());
}

Hope that helps.

鹿童谣 2024-08-18 12:22:29

这应该可以做到:

    // Assuming file1 and file2 are not deeply nested XML files
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc1 = docBuilder.parse(file1);
    Document doc2 = docBuilder.parse(file2);

    // SOLUTION 1: Are the files "similar"?
    Diff diff = new Diff(doc1, doc2);
    System.out.println("Similar (true/false): " + diff.similar());

    // SOLUTION 2: Should you want detailed differences (especially useful for deeply nested files)
    Diff diff = new Diff(doc1, doc2);
    diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); 
    DetailedDiff detailedDiff = new DetailedDiff(diff); 
    System.out.println("Detailed differences: " + detailedDiff.getAllDifferences().toString());

希望有一点帮助。 此处阅读 XMLUnit。

This should do it:

    // Assuming file1 and file2 are not deeply nested XML files
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc1 = docBuilder.parse(file1);
    Document doc2 = docBuilder.parse(file2);

    // SOLUTION 1: Are the files "similar"?
    Diff diff = new Diff(doc1, doc2);
    System.out.println("Similar (true/false): " + diff.similar());

    // SOLUTION 2: Should you want detailed differences (especially useful for deeply nested files)
    Diff diff = new Diff(doc1, doc2);
    diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier()); 
    DetailedDiff detailedDiff = new DetailedDiff(diff); 
    System.out.println("Detailed differences: " + detailedDiff.getAllDifferences().toString());

Hope that helps a bit. Read up on XMLUnit here.

风渺 2024-08-18 12:22:29
diff =
        DiffBuilder.compare(expected)
          .withTest(toBeVerified)
          .ignoreWhitespace()
          .checkForSimilar()
          .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText, ElementSelectors.byName))
          .build();
diff =
        DiffBuilder.compare(expected)
          .withTest(toBeVerified)
          .ignoreWhitespace()
          .checkForSimilar()
          .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText, ElementSelectors.byName))
          .build();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文