需要实现对比两个XML文件中的内容是否一致,并打印出不同的部分功能
一 方法
实现比对两个XML文件,使用 xmlUnit 库,可以比对 xml 的工具。
二 步骤
创建 maven 项目
导入 xmlUnit 库到 pom.xml 文件中
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.6</version>
<scope>compile</scope>
</dependency>
代码实现:
public class compareXML {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
File file1 = new File("对比文件一的路径!");
File file2 = new File("对比文件二的路径!");
Document doc1 = docBuilder.parse(file1);
Document doc2 = docBuilder.parse(file2);
Diff diff = new Diff(doc1, doc2);
diff.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
DetailedDiff detailedDiff = new DetailedDiff(diff);
List list = detailedDiff.getAllDifferences();
for(int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
}
借鉴:https://blog.csdn.net/fengyang182/article/details/109510146