我有 2 个结构几乎相似的 xml 文件,在这些 XML 文件中如何获取以下信息?
- 新添加的控件(或节点)
- 控件(或节点)被移除和
- 控件(或节点)属性已修改。
为了您的信息,我尝试了以下代码:
File f1 = new File(inputXMLPath);
File f2= new File(outputXMLPath);
FileReader fr1 = null;
FileReader fr2 = null;
try {
fr1 = new FileReader(f1);
fr2 = new FileReader(f2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
Diff diff = new Diff(fr1, fr2);
System.out.println("Similar? " + diff.similar());
System.out.println("Identical? " + diff.identical());
DetailedDiff detDiff = new DetailedDiff(diff);
detDiff.overrideDifferenceListener(new DifferenceListener() {
@Override
public int differenceFound(Difference diff) {
if (diff.getId() == DifferenceConstants.CHILD_NODELIST_SEQUENCE_ID
|| diff.getId() == DifferenceConstants.CHILD_NODELIST_LENGTH_ID) {
return RETURN_IGNORE_DIFFERENCE_NODES_IDENTICAL;
}
return RETURN_ACCEPT_DIFFERENCE;
}
@Override
public void skippedComparison(Node arg0, Node arg1) { }
});
List differences = detDiff.getAllDifferences();
for (Object object : differences) {
Difference difference = (Difference)object;
System.out.println("***********************");
System.out.println(difference);
System.out.println("***********************");
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}