尝试获取这两个类似的 XML(想要忽略 xmlns)和不同的元素序列,但对我来说无法正常工作。如果删除 xmlns,则 doc 是 simmilr。我正在使用 XMlUnit 1.5
String s1 = "<root xmlns=\"http:example.com\">"
+"<Date/>"
+"<Time/>"
+"</root>";
String s2 = "<root>"
+"<Time/>"
+"<Date/>"
+"</root>";
myDiff = XMLUnit.compareXML(s1,s2);
Run Code Online (Sandbox Code Playgroud) 我想在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) 我有一些我正在测试的XML,它将日期和时间设置为其中一个元素.这显然会在每次测试运行时发生变化.
有没有办法比较XML并忽略此节点值,或只是用正则表达式或其他东西检查.
我正在测试的XML如下:
<xml-fragment>
<core:date xmlns:core="http://www.company.com/commerce/common/V1/core">2010-06-24T21:41:22.456+01:00</core:date>
<core:serviceName xmlns:core="http://www.company.com/commerce/common/V1/core">calculateNextAndFuturePayments
</core:serviceName>
<core:ackRequired xmlns:core="http://www.company.com/commerce/common/V1/core">false</core:ackRequired>
<v1:viewingCardNumber xmlns:v1="http://www.company.com/commerce/calculateNextAndFuturePayment/V1">405536053
</v1:viewingCardNumber>
</xml-fragment>
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)