I have a scenario where I iterate over a set of rows from a resultset obtained from query
–>QUERY
System.out.println("---Iterating Events---");
while (rs.next()) {
startOfMethod(rs);
}
startOfMethod(ResultSet result)
{
HashMap hmap;
String applicationGroupMap;
String description;
description = result.getString("description"); //description is an XML string
hmap = new XMLRead().xmlDataOutput(description); //This is a separate class which returns a hashmap
applicationGroupMap = (String) hmap.get("Attribute.applicationGroupMap");
}
The problem is that sometimes when it iterates to the second row from first one and the second row does not have a “Attribute.applicationGroupMap, it retains the older value.
Can anyone tell me how do I refresh the hmap on every iteration?
Code which returns Hmap :-
try {
Document doc = convertToDocument(xmlStr);
String hashKey = "";
String hashValue = "";
NodeList listOfKeyValues = doc.getElementsByTagName("k");
for (int s = 0; s < listOfKeyValues.getLength(); s++) {
Node firstNode = listOfKeyValues.item(s);
if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
Element firstElement = (Element) firstNode;
NodeList keyList = firstElement.getChildNodes();
hashKey = ((Node) keyList.item(0)).getNodeValue().trim();
NodeList listOfvalues = doc.getElementsByTagName("v");
Node secondNode = listOfvalues.item(s);
Element valueElement = (Element) secondNode;
NodeList valueList = valueElement.getChildNodes();
if (valueList.getLength() != 0) {
hashValue = ((Node) valueList.item(0)).getNodeValue();
} else {
hashValue = "";
}
hsMap.put(hashKey, hashValue);
}
}
} catch (Throwable t) {
t.printStackTrace();
}
return hsMap;
}
Based on the conversation in the comments, you have two options:
Change the declaration of
hsMapinXMLReadto(Not
static. It might also be more correct to make itprivate, but that’s not related to your problem.)–OR–
In
startOfMethoddoto clear the map after you’ve gotten what you wanted from it.