使用XMLUnit 2,如何在不考虑元素顺序的情况下比较两个文档?
我为XMLUnit 1得到了这个问题,但显然v2中的新API不再具有上述方法了.
这是我目前的代码:
Diff diff = DiffBuilder.compare(expected)
.withTest(actual)
.ignoreComments()
.ignoreWhitespace()
.checkForSimilar()
.build();
assertFalse(diff.hasDifferences());
Run Code Online (Sandbox Code Playgroud)
编辑Stefan Bodewigs评论:
这些是我与上面的片段比较的两个字符串:
String expected = "<root><foo>FOO</foo><bar>BAR</bar></root>";
String actual = "<root><bar>BAR</bar><foo>FOO</foo></root>";
Run Code Online (Sandbox Code Playgroud)
报道的差异
Expected element tag name 'foo' but was 'bar' - comparing <foo...> at /root[1]/foo[1] to <bar...> at /root[1]/bar[1] (DIFFERENT)
Expected text value 'FOO' but was 'BAR' - comparing <foo ...>FOO</foo> at /root[1]/foo[1]/text()[1] to <bar ...>BAR</bar> at /root[1]/bar[1]/text()[1] (DIFFERENT)
Expected element tag name 'bar' but was 'foo' - comparing <bar...> at …Run Code Online (Sandbox Code Playgroud) 我试图覆盖默认的XMLUnit行为,只报告两个输入之间的第一个差异,其中包含发现的所有差异的(文本)报告.
到目前为止我已经完成了这个:
private static void reportXhtmlDifferences(String expected, String actual) {
Diff ds = DiffBuilder.compare(Input.fromString(expected))
.withTest(Input.fromString(actual))
.checkForSimilar()
.normalizeWhitespace()
.ignoreComments()
.withDocumentBuilderFactory(dbf).build();
DefaultComparisonFormatter formatter = new DefaultComparisonFormatter();
if (ds.hasDifferences()) {
StringBuffer expectedBuffer = new StringBuffer();
StringBuffer actualBuffer = new StringBuffer();
for (Difference d: ds.getDifferences()) {
expectedBuffer.append(formatter.getDetails(d.getComparison().getControlDetails(), null, true));
expectedBuffer.append("\n----------\n");
actualBuffer.append(formatter.getDetails(d.getComparison().getTestDetails(), null, true));
actualBuffer.append("\n----------\n");
}
throw new ComparisonFailure("There are HTML differences", expectedBuffer.toString(), actualBuffer.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
但我不喜欢:
Differences客户端代码.DefaultComparisonFormatter并调用.getDetailsnull也许这只是来自一种不合理的坏直觉,但我想知道是否有人对这个用例有一些意见.
如何配置 XmlUnit.Net 在比较两个文档时忽略 XML 声明?
假设我有以下控制文档:
<?xml version="1.0" encoding="utf-8"?>
<a><amount>1</amount></a>
Run Code Online (Sandbox Code Playgroud)
我想比较一下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a><amount>1</amount></a>
Run Code Online (Sandbox Code Playgroud)
比较应该没有差异。
我的期望是使用像这样的 NodeFilter 应该可以工作,但事实并非如此:
var diff = DiffBuilder.Compare(control)
.WithTest(test)
.WithNodeFilter(n => n.NodeType != XmlNodeType.XmlDeclaration)
.Build();
diff.Differences.Count().Should().Be(0);
Run Code Online (Sandbox Code Playgroud)
断言因两个差异而失败 - 一个是编码(大小写不同),另一个是独立属性。我对任何一个都不感兴趣。
我说n.NodeType != XmlNodeType.XmlDeclaration或n.NodeType == XmlNodeType.XmlDeclaration说都没有区别。
我正在使用 XMLUnit.Core v2.5.1。
我想CompareConstraint在我的F#代码中继承XMLUnit.NET 类.
但是,该类只有一个私有构造函数.如果我尝试继承它,我得到这个编译错误:
This 'inherit' declaration specifies the inherited type but no arguments. Consider supplying arguments, e.g. 'inherit BaseType(args)'.
在F#中有没有办法继承没有公共构造函数的类?
我想在groovy代码中比较我的Soap Response和xml文件忽略顺序:
这是我的代码:
import org.custommonkey.xmlunit.Stuff
import org.xmlunit.Stuff
//ExpectedString is my xml converted to text, same for ResponseString
Diff diff = DiffBuilder.compare(ExpectedString)
.withTest(ResponseString)
.ignoreComments()
.ignoreWhitespace()
.checkForSimilar()
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName))
.build();
assertFalse("XML similar " + diff.toString(), diff.hasDifferences())
Run Code Online (Sandbox Code Playgroud)
所以,正如你所看到的,我使用了DefaultNodeMatcher,我使用了XMLUnit2.0 ......没有结果(甚至没有忽略顺序或比较时出现异常错误)
有解决方案吗?解决这个问题
因为我迫切希望找到一个直接的,我可以排序我的xml和我的肥皂反应,所以我可以有一个简单的差异?有没有办法按字母顺序逐行排序?如果有,怎么样?
感谢你们 !
更新:
这是我简化的XML结构
<body>
<stuff>
<miniStuff></miniStuff>
<miniStuff></miniStuff>
</stuff>
<Services>
<Service>
<tag1>ValueA</tag1>
<tag2>ValueAA</tag2>
</Service>
<Service>
<tag1>ValueB</tag1>
<tag2>ValueBB</tag2>
</Service>
</services>
</body>
Run Code Online (Sandbox Code Playgroud)
我的问题是我无法保证ValueA是第一个而不是第二个
我有两个 XML 文件:
<!------------------------File1--------------------------------->
<note id="ignoreThisAttribute_1">
<to>Experts</to>
<from>Matrix</from>
<heading id="dontIgnoreThisAttribute_1">Reminder</heading>
<body>Help me with this problem</body>
</note>
Run Code Online (Sandbox Code Playgroud)
<!------------------------File2--------------------------------->
<note id="ignoreThisAttribute_2">
<to>Experts</to>
<from>Matrix</from>
<heading id="dontIgnoreThisAttribute_2">Reminder</heading>
<body>Help me with this problem</body>
</note>
Run Code Online (Sandbox Code Playgroud)
在比较这两个文件时,我必须忽略idNode: 的属性: 。note
我在用DiffBuilder:
Diff documentDiff = DiffBuilder.compare(srcFile).withTest(destFile).build()
Run Code Online (Sandbox Code Playgroud)
大多数在线解决方案建议实施DifferenceEvaluator:
也尝试过,但这会忽略具有属性 id 的所有节点,而我想忽略特定节点的属性:
Diff documentDiff = DiffBuilder.compare(srcFile).withTest(destFile).build()
Run Code Online (Sandbox Code Playgroud)
在我的测试类中调用方法:
public class IgnoreAttributeDifferenceEvaluator implements DifferenceEvaluator {
private String attributeName;
public IgnoreAttributeDifferenceEvaluator(String attributeName) {
this.attributeName = attributeName;
}
@Override
public ComparisonResult evaluate(Comparison comparison, ComparisonResult outcome) {
if (outcome …Run Code Online (Sandbox Code Playgroud) 我需要使用XMLUnit 2xml 子节点顺序不同的位置来比较 XML 文件。由于使用了底层库,我无法影响子节点的顺序。
为了进行比较,我正在使用:
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.0.0-alpha-02</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-matchers</artifactId>
<version>2.0.0-alpha-02</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
问题归结为这个 JUnit 测试:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.xmlunit.builder.Input.fromString;
import static org.xmlunit.diff.ElementSelectors.byName;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.xmlunit.diff.DefaultNodeMatcher;
import java.io.IOException;
public class XmlTest {
@Test
public void test() throws Exception {
String t1 = fromClassPath("t1.xml");
String t2 = fromClassPath("t2.xml");
assertThat(fromString(t1), isSimilarTo(fromString(t2)).withNodeMatcher(new DefaultNodeMatcher(byName)));
}
private static String fromClassPath(String fileName) throws IOException {
return IOUtils.toString(new ClassPathResource(fileName).getInputStream());
} …Run Code Online (Sandbox Code Playgroud)