我目前正在开发一个程序,它读取两个xml文件的所有元素并将它们相互比较。之后它会生成一个包含差异的csv(这不是问题)。
我能够阅读一些数据并进行比较,但我认为我的比较“算法”并不是最有效的。你知道更好的解决方案吗?
可用的内容:
代码目前的状态:
public static void CompareXMLELEMENTNAME(ArrayList<XMLELEMENTNAME> elementList1, ArrayList<XMLELEMENTNAME> elementList2){
ArrayList<String> strinls1 = new ArrayList<String>();
ArrayList<String> strinls2 = new ArrayList<String>();
ArrayList<String> notInF1 = new ArrayList<String>();
ArrayList<String> notInF2 = new ArrayList<String>();
ArrayList<XMLELEMENTNAME> ListSameonBothFile1 = new ArrayList<XMLELEMENTNAME>();
ArrayList<XMLELEMENTNAME> ListSameonBothFile2 = new ArrayList<XMLELEMENTNAME>();
//Create a list with the Name of each element in: elementlist1
for(int i = 0; i < List1.size(); i++){
XMLELEMENTNAME s1 = List1.get(i);
strinls1.add(s1.getELEMENTNAME());
}
//Create a list with the Name of each element in: elementlist2
for(int i = 0; i < List2.size(); i++){
XMLELEMENTNAME s2 = List2.get(i);
strinls2.add(s2.getELEMENTNAME());
}
//Scann the 2 files for the elements which exist in both files
for (String a : strinls1){
notInF2.add(strinls2.contains(a) ? a + "SAME" : a);
if(strinls2.contains(a)){
int i = strinls2.indexOf(a);
ListSameonBothFile1.add(List2.get(i));
}
}
for (String a : strinls2){
notInF1.add(strinls1.contains(a) ? a + "SAME" : a);
if(strinls1.contains(a)){
int i = strinls1.indexOf(a);
ListSameonBothFile2.add(List1.get(i));
}
}
//Call The
for(int i = 0; i < ListSameonBothFile1.size(); i++){
heavyCompareChildElement(ListSameonBothFile1.get(i),ListSameonBothFile2.get(i));
}
CreateCsv.generateCsvFile("unterschiede.csv");
}
public static void heavyCompareChildElement(UNDERELEMENT s1, UNDERELEMENT s2){
HashMap<String, String> hashList1 = new HashMap<String, String>();
HashMap<String, String> hashList2 = new HashMap<String, String>();
//CREATE hashmap with all the parameters and values of the to compare ellement
hashList1.put("TAGNAME",s1.getGETTAG());
hashList1.put("TIME",s1.getTIME);
//CREATE hashmap with all the parameters and values of the to compare ellement
hashList2.put("TAGNAME",s2.getGETTAG());
hashList2.put("TIME",s2.getTIME);
//COMPARE and putting the found diferences in a Summary object
for (Entry<String, String> entry: hashList1.entrySet()) {
// Check if the current value is a key in the 2nd map
if (!hashList2.containsValue(entry.getValue())) {
Summary sum = new Summary();
sum.setType("UNDERELEMENT");
sum.setSuperName(hashList1.get("NAME"));
sum.setName("");
sum.setOLD(entry.getValue());
sum.setNEW(hashList2.get(entry.getKey()));
sum.setAttribute(entry.getKey());
Main.addElement(sum);
}
}
}
这就是我必须为主要xml元素的每个元素做的,这是一个代码分配,几乎有+ - 80个不同的XML对象。
XML看起来像这样:
<MAINELEMENT>
<UNDERELEMENT KEY1="value" KEY2="value" KEY3="value" KEY4="value">
<UNDEROPTIONOFUNDERELEMENT NAME="asdas" OPTIONS="sdasd" OPTIONS1="1"/>
<ANOTHERELEMENT NAME="wefrwer" SOMEOPTIONS="fkwhjewjkh" >
<UNDERELEMENT1 NAME="blblbl" type ="bkdk">
<UNDERUNDERUNDERELEMENT NAME="blbalba"/>
</ON>
<UNDERELEMENT2 NAME="blblablbal"/>
</ANOTHERELEMENT>
<MAINELEMENT/>
我真的很感激更有效的解决方案。我知道XMLunit,但无法弄清楚方法。
答案 0 :(得分:1)
使用简单的XMLUnit
public void testIdenticalAndSimilar() throws Exception {
String controlXML = "<account><id>3A-00</id><name>acme</name></account>";
String testXML = "<account><name>acme</name><id>3A-00</id></account>";
Diff diff = new Diff(controlXML, testXML);
assertTrue(diff.similar());
assertFalse(diff.identical());
}
similar
表示标签内的排序无关紧要。 identical
限制性更强。
这是来自http://xmlunit.sourceforge.net/example.html的XMLUnits示例代码的示例。使用DetailedDiff
之后,您可以检索所有检测到的差异的列表。