使用xmlunit定制输出消息的XML比较

时间:2015-02-05 18:45:13

标签: java xml xml-parsing xmlunit

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

reference.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>

compare.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个书节点。

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

 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]和at books [1]



预期文字价值' '但是'     ' - 比较  at / books [1] / text()[3] to      at / books [1] / text()[3]



预期存在子节点'null'但是'book' - 比较null和at books [1] / book [3]



预期存在子节点'null'但是'#text' - 在null处比较  at / books [1] / text()[4]


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

2 个答案:

答案 0 :(得分:1)

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

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) { }
        });

答案 1 :(得分: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课程(主要方法):

    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);
        }

    }