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!