我是这个领域的新手,并被分配了一项具有挑战性的任务。我有 2 个 XML 文件(即下面提到的 Test1 和 Test 2)。我需要使用 java 比较这些 xml 文件,如果没有差异,则将布尔值返回为 true。这里的挑战是第二个 Xml 文件(Test2.xml),元素顺序总是混乱的。我尝试使用 XMLUnit2 来比较 2 个 xml 字符串,但如果有多个父节点或大型 xml,它就会失败。
测试1.Xml:
Test1.Xml:
<PACKINGS>
<PACKING>
<TYPE>CCST</TYPE>
<ORDERNUM>810000510</ORDERNUM>
<SVCTAGS>
<SVCTAG>
<SVCTAGTYPE>DRAGON</SVCTAGTYPE>
<SVCTAGNUMBER>768100005105001</SVCTAGNUMBER>
<TIENUMBER>1</TIENUMBER>
<BOXID>768100005105001</BOXID>
<LENGTH>4</LENGTH>
<WIDTH>5</WIDTH>
<HEIGHT>10</HEIGHT>
<PARTS>
<PART>
<PARTNUMBER>RKH5D</PARTNUMBER>
<PARTQTY>10</PARTQTY>
</PART>
</PARTS>
</SVCTAG>
<SVCTAG>
<SVCTAGTYPE>DRAGON</SVCTAGTYPE>
<SVCTAGNUMBER>768100005105002</SVCTAGNUMBER>
<TIENUMBER>2</TIENUMBER>
<BOXID>768100005105002</BOXID>
<LENGTH>4</LENGTH>
<WIDTH>5</WIDTH>
<HEIGHT>10</HEIGHT>
<PARTS>
<PART>
<PARTNUMBER>FHMTN</PARTNUMBER>
<PARTQTY>10</PARTQTY>
</PART>
</PARTS>
</SVCTAG>
</SVCTAGS>
</PACKING>
</PACKINGS>
Test2.Xml:
<PACKINGS>
<PACKING>
<TYPE>CCST</TYPE>
<ORDERNUM>810000510</ORDERNUM>
<SVCTAGS>
<SVCTAG>
<SVCTAGTYPE>DRAGON</SVCTAGTYPE>
<SVCTAGNUMBER>768100005105002</SVCTAGNUMBER>
<TIENUMBER>2</TIENUMBER>
<BOXID>768100005105002</BOXID>
<LENGTH>4</LENGTH>
<WIDTH>5</WIDTH>
<HEIGHT>10</HEIGHT>
<PARTS>
<PART>
<PARTNUMBER>FHMTN</PARTNUMBER>
<PARTQTY>10</PARTQTY>
</PART>
</PARTS>
</SVCTAG>
<SVCTAG>
<SVCTAGTYPE>DRAGON</SVCTAGTYPE>
<SVCTAGNUMBER>768100005105001</SVCTAGNUMBER>
<TIENUMBER>1</TIENUMBER>
<BOXID>768100005105001</BOXID>
<LENGTH>4</LENGTH>
<WIDTH>5</WIDTH>
<HEIGHT>10</HEIGHT>
<PARTS>
<PART>
<PARTNUMBER>RKH5D</PARTNUMBER>
<PARTQTY>10</PARTQTY>
</PART>
</PARTS>
</SVCTAG>
</SVCTAGS>
</PACKING>
</PACKINGS>
以下是我尝试过的代码,但使用 xml 单元失败:
package com.com.java;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLAssert;
import org.testng.annotations.Test;
import org.xmlunit.builder.DiffBuilder;
import org.xmlunit.builder.Input;
import org.xmlunit.diff.DefaultNodeMatcher;
import org.xmlunit.diff.ElementSelectors;
import org.xmlunit.matchers.CompareMatcher;
public class TestNg {
@Test
public void testXmlUnit() {
String ControlXML = "<PACKINGS><PACKING><TYPE>CCST</TYPE><ORDERNUM>810000510</ORDERNUM><SVCTAGS><SVCTAG><SVCTAGTYPE>DRAGON</SVCTAGTYPE><SVCTAGNUMBER>768100005105002</SVCTAGNUMBER><TIENUMBER>2</TIENUMBER><BOXID>768100005105002</BOXID><LENGTH>4</LENGTH><WIDTH>5</WIDTH><HEIGHT>10</HEIGHT></SVCTAG><SVCTAG><SVCTAGTYPE>DRAGON</SVCTAGTYPE><SVCTAGNUMBER>768100005105001</SVCTAGNUMBER><TIENUMBER>1</TIENUMBER><BOXID>768100005105001</BOXID><LENGTH>4</LENGTH><WIDTH>5</WIDTH><HEIGHT>10</HEIGHT><PARTS><PART><PARTNUMBER>RKH5D</PARTNUMBER><PARTQTY>10</PARTQTY></PART></PARTS></SVCTAG></SVCTAGS></PACKING></PACKINGS>";
String testXml = "<PACKINGS><PACKING><TYPE>CCST</TYPE><ORDERNUM>810000510</ORDERNUM><SVCTAGS><SVCTAG><SVCTAGTYPE>DRAGON</SVCTAGTYPE><SVCTAGNUMBER>768100005105001</SVCTAGNUMBER><TIENUMBER>1</TIENUMBER><BOXID>768100005105001</BOXID><LENGTH>4</LENGTH><WIDTH>5</WIDTH><HEIGHT>10</HEIGHT></SVCTAG><SVCTAG><SVCTAGTYPE>DRAGON</SVCTAGTYPE><SVCTAGNUMBER>768100005105002</SVCTAGNUMBER><TIENUMBER>2</TIENUMBER><BOXID>768100005105002</BOXID><LENGTH>4</LENGTH><WIDTH>5</WIDTH><HEIGHT>10</HEIGHT><PARTS><PART><PARTNUMBER>FHMTN</PARTNUMBER><PARTQTY>10</PARTQTY></PART></PARTS></SVCTAG></SVCTAGS></PACKING></PACKINGS>";
assertThat(testXml, CompareMatcher.isSimilarTo(ControlXML).ignoreWhitespace().normalizeWhitespace().withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byNameAndText, ElementSelectors.byName)));
}
}