Java 使用xmlunit自定义输出消息进行XML比较

Java 使用xmlunit自定义输出消息进行XML比较,java,xml,xml-parsing,xmlunit,Java,Xml,Xml Parsing,Xmlunit,您好,我正在尝试使用xmlunit比较两个xml文件的内容 这是我的输入XML reference.xml <?xml version="1.0" encoding="UTF-8"?> <books> <book> <name>abc</name> <isbn>9971-5-0210-0</isbn> <author>abc</author

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

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>
<?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>
输出:

**Total differences:-->4

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



预期文本值' “但是是” “-比较 at/books[1]/text()[3]到 at/books[1]/text()[3]



应存在子节点“null”,但为“book”-将null处的值与/books[1]/book[3]处的值进行比较



应存在子节点“null”,但为“#text”-在null处与 at/books[1]/text()[4]



而不是有任何方式,所以我可以把变化看作只有1(因为我认为只有一个书节点被添加忽略内部标签),并且也定制输出到我们定制的消息

< p>一个解决方案可以是将XML文件解析成java对象,然后比较它们。p> 在本例中,我没有使用xmlunit,它基于,我希望它能帮助您:

图书班

    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类(带主方法):

公共类书籍{
私人最终清单书;
公共书籍(){
books=新数组列表();
}
公共作废添加(最终b册){
增加(b);
}
公共列表getBooks(){
还书;
}
@凌驾
公共字符串toString(){
还书;
}
公共静态void main(最终字符串[]args){
final XStream XStream=new XStream();
别名(“books”,books.class);
别名(“book”,book.class);
xstream.addImplicitCollection(Books.class,“Books”);
final Books ref=(Books)xstream.fromXML(Book.class.getClassLoader().getResourceAsStream(“reference.xml”);
最终图书比较=(图书)xstream.fromXML(Book.class.getClassLoader().getResourceAsStream(“compare.xml”);
系统输出打印项次(参考);
System.out.println(比较);
最终列表rbooks=newarraylist(参考getBooks());
最终列表cbooks=newarraylist(compare.getBooks());
rbooks.removeAll(cbooks);
System.out.println(“比较中缺少书籍:+rbooks”);
rbooks.clear();
arbooks.addAll(参考getBooks());
cbooks.removeAll(rbooks);
System.out.println(“比较中的额外书籍:+cbooks”);
}
}

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

XMLUnit.setIgnoreWhitespace(true);
为了抑制另外两个差异中的一个,您需要覆盖
差异Listener
,并显式忽略其中一个差异。从您描述的内容中,您希望只看到未找到的
子节点
差异

    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) { }
        });
XMLUnit.setIgnoreWhitespace(true);
    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) { }
        });