While doing my assignment i ran into a rather strange error and can't find why my HashMap gets emptied every time.
Im in no way asking for a solution for the named assignment i just need help with this HashMap.
So what i am trying to do is to read a RSS Feed with the SAX API and save every match into a HashMap called 'results'. The second hashmap is for storing the current item i am reading.
public class SAX extends DefaultHandler{
HashMap<Integer, HashMap<Integer,String>> results = new HashMap<Integer, HashMap<Integer, String>>();
HashMap<Integer, String> current_item = new HashMap<Integer,String>();
int flag = 0;
String[] filter = {"Corona"};
String current = "";
int cnt_results, cnt_item = 0;
public static void main(String[]args) throws ParserConfigurationException, SAXException, IOException {
Init of the Sax Stuff here. This is normal stuff.
But i use the SAX Class as a Handler.
}
public void startDocument() {
System.out.println("Started the RSS Feed...");
}
public void endDocument() {
for(int i = 0; i<this.results.size();++i) {
Map<Integer,String> map = this.results.get(i);
for(int i2 = 0; i2<map.size();++i2) {
System.out.println(map.get(i2));
}
}
System.out.println("Seems like we hit the end.");
}
public void startElement(String uri,String localName, String qName, Attributes attributes) throws SAXException {
if(localName == "item") {
flag = 1;
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
current_item.put(cnt_item, current);
cnt_item++;
// Filters if description contains one of the Keywords
if(localName == "description") {
for(int i = 0; i<filter.length;++i) {
if(current.contains(filter[i])) {
flag = 2;
break;
}
}
}
if(localName == "item") {
if(flag == 2) {
if(this.results.containsValue(current_item)) {
;
}else {
this.results.put(cnt_results, current_item);
cnt_results++;
}
}
flag = 0;
cnt_item = 0;
current_item.clear();
}
}
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
}
public void characters(char[] ch, int start, int length) throws SAXException{
current = new String(ch,start,length);
}
}
The Reader should then go over the RSS Feed and find every Item that has a description containing the filter word and save them into the results Map.
When trying to read these saved results i just get blank lines.
Maybe that's not the best way to express what it means so i just gave you the whole code.
Glad for your help. Thanks!
localName == "item"
localName == "description"
This compares reference identity. As in, "item" == "item"
is false. You want "item".equals(localName)
which compares the string content.
current_item.clear();
Java is reference based. current_item
isn't the hashmap. It's a reference to the map. Your results
hashmap also stores references, not maps. It's like treasure maps: everything is a map to the treasure, not treasure. .
is: Follow the map, and dig down. so, current_item.clear()
follows the map,digs down, and destroys the contents of the treasure you find. Your results
hashmap has its own copy of a treasure map, but it leads to the same treasure, so you probably don't want this. Try current_item = new HashMap<>();
. Instead of following the map and destroying the treasure, this creates a new treasure, buries it in the sand, and updates your map (And not any copies of this map, they remain what they were) to point at the newly buried treasure.