我有以下带有客户列表的 XML,每个客户都有交易列表。我已经设法编写 XMLUnit 代码来忽略客户的顺序而不是交易。交易有一些独特的结构(不能改变)。以下是示例 XML 和代码。请帮忙。
XML 1:
<Customers>
<Customer>
<Id>1</Id>
<Transactions>
<Transaction>
<CashTransaction>
<amount>100</amount>
</CashTransaction>
</Transaction>
<Transaction>
<CardTransaction>
<amount>200</amount>
</CardTransaction>
</Transaction>
</Transactions>
</Customer>
<Customer>
<Id>2</Id>
<Transactions>
<Transaction>
<CardTransaction>
<amount>500</amount>
</CardTransaction>
</Transaction>
<Transaction>
<CashTransaction>
<amount>400</amount>
</CashTransaction>
</Transaction>
</Transactions>
</Customer>
</Customers>
XML 2:
<Customers>
<Customer>
<Id>2</Id>
<Transactions>
<Transaction>
<CashTransaction>
<amount>400</amount>
</CashTransaction>
</Transaction>
<Transaction>
<CardTransaction>
<amount>500</amount>
</CardTransaction>
</Transaction>
</Transactions>
</Customer>
<Customer>
<Id>1</Id>
<Transactions>
<Transaction>
<CardTransaction>
<amount>200</amount>
</CardTransaction>
</Transaction>
<Transaction>
<CashTransaction>
<amount>100</amount>
</CashTransaction>
</Transaction>
</Transactions>
</Customer>
</Customers>
代码:
Diff diffs = DiffBuilder.compare(Input.from(resourceAsStream1))
.withTest(Input.from(resourceAsStream2))
.ignoreComments()
.ignoreWhitespace()
.normalizeWhitespace()
.checkForSimilar()
.withDifferenceEvaluator(evaluator)
.withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.conditionalBuilder()
.whenElementIsNamed("Customer")
.thenUse(ElementSelectors.byXPath("./Id", ElementSelectors.byNameAndText))
.elseUse(ElementSelectors.byName)
.build()
)).build();
显示的差异:
Expected child 'CashTransaction' but was 'null' - comparing <CashTransaction...> at /Customers[1]/Customer[1]/Transactions[1]/Transaction[1]/CashTransaction[1] to <NULL>
Expected child 'null' but was 'CardTransaction' - comparing <NULL> to <CardTransaction...> at /Customers[1]/Customer[2]/Transactions[1]/Transaction[1]/CardTransaction[1]
Expected child 'CardTransaction' but was 'null' - comparing <CardTransaction...> at /Customers[1]/Customer[1]/Transactions[1]/Transaction[2]/CardTransaction[1] to <NULL>
Expected child 'null' but was 'CashTransaction' - comparing <NULL> to <CashTransaction...> at /Customers[1]/Customer[2]/Transactions[1]/Transaction[2]/CashTransaction[1]
..
..