【问题标题】:XML comparision using xmlunit customizing output message使用 xmlunit 自定义输出消息的 XML 比较
【发布时间】:2015-02-05 18:45:13
【问题描述】:

您好,我正在尝试使用 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] 与 /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(因为我认为只添加了一个书节点而忽略了内部标签),并且还可以将输出自定义为我们的自定义消息

【问题讨论】:

    标签: java xml xml-parsing xmlunit


    【解决方案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) { }
            });
    

    【讨论】:

      【解决方案2】:

      一种解决方案可能是将 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);
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-28
        • 2020-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多