I have an XML file which contains multiple nodes that have children. If the nodes have the same keys, their children has to be merged into one node.
<availexport> <date>090423121513</date> <employee emp_cd='9120004'> <day date='20050406' start='10' end='20'/> <day date='20050406' start='21' end='23'/> <day date='20050511' start='12' end='23'/> </employee> <records>3</records> </availexport>
Two records here have the same dates. How can I merge them into one? I have a list of these as key/value pairs.
for (int k = this.employeesList.size(); k > 0; k--) { Map empInfo1 = new HashMap(); Map empInfo = (Map) this.employeesList.remove(0); this.empID = (Long) empInfo.get(EMP_ID); this.hrID = (String) empInfo.get(HR_EMP_ID); this.avail_iDate = (Long) empInfo.get(AVAIL_IDATE); this.start_ITime = (Long) empInfo.get(START_ITIME); this.end_ITime = (Long) empInfo.get(END_ITIME); List availList = new ArrayList(); Map availList1 = new HashMap(); if (empAvailRecord.containsKey(empID)) { empInfo1 = (Map) empAvailRecord.get(empID); availList = (List) empInfo1.get('DAY'); availList1.put(AVAIL_IDATE, avail_iDate); availList1.put(START_ITIME, start_ITime); availList1.put(END_ITIME, end_ITime); availList.add(availList1); } else { availList1.put(AVAIL_IDATE, avail_iDate); availList1.put(START_ITIME, start_ITime); availList1.put(END_ITIME, end_ITime); availList.add(availList1); empInfo1.put('HR_ID', hrID); empInfo1.put('DAY', availList); empAvailRecord.put(empID, empInfo1); } }
You should check for repititions before you add availList1 to availList. You can write a function which traverses availList, gets avail_idate and matches it with the one you are about to add. How much records can be there in the list generally. Because you will need to consider peformance factors if traversing sounds costly.