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)
  • 相关阅读:
    面向对象三大特性五大原则
    如何快速的浏览项目
    网页测速
    截取字符串
    iOS-tableView点击下拉菜单
    iOS_block内存分析
    iOS_ @property参数分析
    iOS-设计模式之Block
    iOS-设计模式之代理反向传值
    iOS-设计模式之通知
  • 原文地址:https://www.cnblogs.com/zno2/p/7120369.html
Copyright © 2011-2022 走看看