【问题标题】:How to diff-compare two xml files using java如何使用java比较两个xml文件
【发布时间】:2012-12-25 10:43:33
【问题描述】:

我有两个要比较的 xml 文件:

old.xml:

<EMPLOYEES>
  <employee>
    <id>102</id>
    <name>Fran</name>
    <department>  THIS IS COMPUTER DEPARTMENT  </department>
  </employee> 
  <employee>
    <id>105</id>
    <name>Matthew</name>
    <department> THIS IS SCIENCE AND TECHNOLOGY </department>
  </employee> 
</EMPLOYEES>

new.xml:

<EMPLOYEES>
  <employee>
    <id>105</id>
    <name>Matthew</name>
    <department>  THIS IS SCIENCE AND TECHNOLOGY **Modified *** </department>
  </employee> 
  <employee>
    <id>106</id>
    <name>xyz</name>
    <department> THIS IS SCIENCE AND TECHNOLOGY </department>
  </employee>
  <employee>
    <id>107</id>
    <name>Francis</name>
    <department>  THIS IS XYZ  </department>
  </employee>
</EMPLOYEES>

我想比较这两个文件并返回添加、删除或更新了哪些记录。 old.xml 包含 2 个 &lt;employee&gt; 记录,new.xml 包含 3 个 &lt;employee&gt; 记录。

我希望结果是这样的:

添加的记录 2:例如:-employee.id=106 和employee.id=107

已删除记录 1:例如:-employee.id=102

更新记录 1: ex:- employee.id=105 更新为----

区分这两个 XML 文件以获得这些结果的最佳方法是什么?

【问题讨论】:

  • 改进您的格式。我尝试为您编辑,但不知何故被拒绝了。
  • 这个问题措辞太含糊。 “我面临一些问题”是指您遇到了特定问题,还是只是希望有人告诉您如何实现检测元素更新? (您似乎想通过id 检测更新,并没有真正的通用解决方案,因为它与 XML 的 结构 无关,但它在您的应用程序中的含义.)

标签: java xml diff


【解决方案1】:

这听起来类似于Best way to compare 2 XML documents in Java。我建议检查 XMLUnit:

http://xmlunit.sourceforge.net/

【讨论】:

    【解决方案2】:

    我会做什么

    @XmlRootElement
    class Employees {
        List<Employee> list;
    }
    
    class Employee {
        int id;
        String name;
        String department;
    }
    

    解组 xml。创建 2 个地图并执行以下操作

        Map<Integer, Employee> map1 = ...
        Map<Integer, Employee> map2 = ...
                    // see Map.retainAll API
        map1.keySet().retainAll(map2.keySet());
        // now map1 contains common employees
        for (Integer k : map1.keySet()) {
            Employee e1 = map1.get(k);
            Employee e2 = map2.get(k);
            // compare name and department
        }
    

    【讨论】:

      【解决方案3】:

      对于逻辑差异,即两个xml文件中对应节点的差异,可以使用节点类的isEqualNode(Node node)方法。

      对于逐行比较,扫描仪易于使用。示例代码 -

          public void compareFiles (Scanner file1, Scanner file2) {
                      String lineA ;
                      String lineB ;
      
                      int x = 1;
      
                          while (file1.hasNextLine() && file2.hasNextLine()) {
                              lineA = file1.nextLine();
                              lineB = file2.nextLine();
      
                              if (!lineA.equals(lineB)) {
                                  System.out.print("Diff " + x++ + "\r\n");
                                  System.out.print("< " + lineA + "\r\n");
                                  System.out.print("> " + lineB + "\r\n");
                              }
                          }
      
                  } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-28
        相关资源
        最近更新 更多