zoukankan      html  css  js  c++  java
  • 如何比较两个xml 的异同

    http://www.xmlunit.org/

    <dependency>
        <groupId>org.xmlunit</groupId>
        <artifactId>xmlunit-core</artifactId>
        <version>2.3.0</version>
    </dependency>
    <root>
        <id>123</id>
        <name>liming</name>
    </root>
    control.xml
    <root>
        <id>1234</id>
        <name>liming</name>
    </root>
    test.xml
    import static org.junit.Assert.assertFalse;
    
    import java.io.File;
    
    import javax.xml.transform.Source;
    
    import org.junit.Test;
    import org.xmlunit.builder.DiffBuilder;
    import org.xmlunit.builder.Input;
    import org.xmlunit.diff.Diff;
    import org.xmlunit.diff.Difference;
    
    public class Xmltest {
    
        @Test
        public void ff() {
            File control = new File("E://control.xml");
            File test = new File("E://test.xml");
            
            Source sourceControl = Input.fromFile(control).build();
            Source sourceTest = Input.fromFile(test).build();
            
            Diff build = DiffBuilder.compare(sourceControl).withTest(sourceTest).ignoreComments().ignoreWhitespace().build();
            
            boolean hasDifferences = build.hasDifferences();
            if (hasDifferences) {
                Iterable<Difference> differences = build.getDifferences();
                for (Difference difference : differences) {
                    System.out.println(difference);
                }
            }
            assertFalse(hasDifferences);
        }
    
    }

    运行结果:

    Expected text value '123' but was '1234' - comparing <id ...>123</id> at /root[1]/id[1]/text()[1] to <id ...>1234</id> at /root[1]/id[1]/text()[1] (DIFFERENT)
  • 相关阅读:
    java基础
    C++菜鸟启动之旅--vc6.0使用教程详解
    第8章 Linux磁盘与文件系统管理
    IO(四)----对象的序列化
    IO(三)----序列流
    IO(二)----字符流
    IO(一)----字节流
    File类
    枚举类
    自动装箱和自动拆箱
  • 原文地址:https://www.cnblogs.com/zno2/p/7120369.html
Copyright © 2011-2022 走看看