0

您好我正在尝试使用 xmlunit 比较两个 xml 文件的内容
这是我的输入 xmls

参考.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <name>abc</name>
        <isbn>9971-5-0210-0</isbn>
        <author>abc</author>
        <category></category>
    </book>
    <book>
        <name>def</name>
        <isbn>9971-5-0222-0</isbn>
        <author>def</author>
    </book>
</books>

比较.xml

<?xml version="1.0" encoding="UTF-8"?>
    <books>
        <book>
            <name>abc</name>
            <isbn>9971-5-0210-0</isbn>
            <author>abc</author>
            <category></category>
        </book>
        <book>
            <name>def</name>
            <isbn>9971-5-0222-0</isbn>
            <author>def</author>
        </book>
        <book>
            <name>ghi</name>
            <isbn>9971-5-0222-0</isbn>
            <author>test authora</author>
        </book>
    </books>

在这里我们可以观察到compare.xml有 3 个 book 节点。

我正在打印总差异并计算如下

 DetailedDiff detDiff = new DetailedDiff(diff);
    List differences = detDiff.getAllDifferences();
    System.out.println("Total differences:-->"+differences.size());
    for (Object object : differences) {
        Difference difference = (Difference)object;
        System.out.println("***********************");
        System.out.println(difference);
        System.out.println("***********************");
    }

输出:

**Total differences:-->4

子节点的预期数量为“5”,但为“7” - 在 /books[1] 与 /books[1] 进行比较



预期文本值 ' ' 但为 ' ' - 在 /books[1]/text()[3] 与 /books[1]/text()[3] 进行比较



预期存在子节点“null”但为“book” - 在 null 与 /books[1]/book[3] 进行比较



子节点“null”的预期存在但为“#text” - 在 null 与 /books[1]/text()[4] 进行比较


相反,有什么办法可以让我将更改视为只有 1(因为我认为只添加了一个书节点,而忽略了内部标签),并且还可以将输出自定义为我们的自定义消息

4

2 回答 2

2

第一步是忽略元素内容空白,这将消除第二个和第四个差异。

XMLUnit.setIgnoreWhitespace(true);

为了抑制其他两个差异之一,您需要覆盖DifferenceListener并明确忽略其中一个。根据您的描述,您更愿意只看到CHILD_NODE_NOT_FOUND差异。

    detDiff.overrideDifferenceListener(new DifferenceListener() {
            @Override
            public int differenceFound(Difference difference) {
                return difference.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID
                    ? RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL
                    : RETURN_ACCEPT_DIFFERENCE;
            }
            @Override
            public void skippedComparison(Node control, Node test) { }
        });
于 2015-02-06T05:18:20.063 回答
0

一种解决方案可能是将 xml 文件解析为 java 对象并稍后进行比较。

在这个例子中我没有使用 xmlunit,它是基于xstream的,希望对你有所帮助:

书类

    public class Book {

        private String  name;
        private String  isbn;
        private String  author;
        private String  category;

        public String getName() {
            return name;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public String getIsbn() {
            return isbn;
        }

        public void setIsbn(final String isbn) {
            this.isbn = isbn;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(final String author) {
            this.author = author;
        }

        public String getCategory() {
            return category;
        }

        public void setCategory(final String category) {
            this.category = category;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (author == null ? 0 : author.hashCode());
            result = prime * result + (category == null ? 0 : category.hashCode());
            result = prime * result + (isbn == null ? 0 : isbn.hashCode());
            result = prime * result + (name == null ? 0 : name.hashCode());
            return result;
        }

        @Override
        public boolean equals(final Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Book other = (Book) obj;
            if (author == null) {
                if (other.author != null) {
                    return false;
                }
            } else if (!author.equals(other.author)) {
                return false;
            }
            if (category == null) {
                if (other.category != null) {
                    return false;
                }
            } else if (!category.equals(other.category)) {
                return false;
            }
            if (isbn == null) {
                if (other.isbn != null) {
                    return false;
                }
            } else if (!isbn.equals(other.isbn)) {
                return false;
            }
            if (name == null) {
                if (other.name != null) {
                    return false;
                }
            } else if (!name.equals(other.name)) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            return "Book [name=" + name + ", isbn=" + isbn + ", author=" + author + ", category=" + category + "]";
        }

    }

Boks 类(带有 main 方法):

    public class Books {

        private final List<Book>    books;

        public Books() {
            books = new ArrayList<Book>();
        }

        public void add(final Book b) {
            books.add(b);
        }

        public List<Book> getBooks() {
            return books;
        }

        @Override
        public String toString() {
            return books.toString();
        }

        public static void main(final String[] args) {
            final XStream xstream = new XStream();
            xstream.alias("books", Books.class);
            xstream.alias("book", Book.class);
            xstream.addImplicitCollection(Books.class, "books");
            final Books ref = (Books) xstream.fromXML(Book.class.getClassLoader().getResourceAsStream("reference.xml"));
            final Books compare = (Books) xstream.fromXML(Book.class.getClassLoader().getResourceAsStream("compare.xml"));
            System.out.println(ref);
            System.out.println(compare);
            final List<Book> rbooks = new ArrayList<Book>(ref.getBooks());
            final List<Book> cbooks = new ArrayList<Book>(compare.getBooks());
            rbooks.removeAll(cbooks);
            System.out.println("Missing books in compare : " + rbooks);
            rbooks.clear();
            rbooks.addAll(ref.getBooks());
            cbooks.removeAll(rbooks);
            System.out.println("Extra books in compare : " + cbooks);
        }

    }
于 2015-02-05T20:41:31.940 回答