I am having trouble storing data in Arraylist of Hashmap which is parsed from a local xml file.
First of all, here is my xml file,
<?xml version="1.0"?>
<organization>
<employee>
<title>Harry0</title>
<link>Smith0</link>
<date>hs0</date>
<salary>200000-0</salary>
</employee>
<employee>
<title>Harry1</title>
<link>Smith1</link>
<date>hs1</date>
<salary>300000-1</salary>
</employee>
<employee>
<title>Harry2</title>
<link>Smith2</link>
<date>hs2</date>
<salary>300000-2</salary>
</employee>
</organization>
I want to store each pair of title
, link
and date
in Hashmap and then put it inside array list of Hashmap.
I am using SAX parser but I couldn't be able to achieve what I want to do,
Here is my code for declaring a Hashmap and ArrayList of Hashmaps,
ArrayList<HashMap<String, String>> xml_Array = new ArrayList<HashMap<String,String>>();
HashMap<String, String> keyValuePair = new HashMap<String, String>();
Now What I did is,
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
startElement = localName;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
elementValue = new String(ch, start, length);
}
@Override
public void endElement(String uri, String endElement, String qName)
throws SAXException {
super.endElement(uri, endElement, qName);
if (startElement == endElement){
inElements = true;
}
if (inElements == true){
if (endElement == "title"){
keyValuePair.put("title", elementValue);
}
else if (endElement == "link"){
keyValuePair.put("link", elementValue);
}
else if (endElement == "date"){
keyValuePair.put("date", elementValue);
}
inElements = false;
}
xml_Array.add(keyValuePair);
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
Log.e("test",xml_Array + "");
}
In endElement
method of Sax Parser, I checked if the element is either title
, date
or link
and then put its data in a hashmap and eventually add it inside array list, but the data is being overridden in arraylist instead of appending,
Here is my output from Logcat,
[{date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}, {date=hs2, title=Harry2, link=Smith2}]
What the only last employee details are added in arraylist so many times ? and why the employee details of first two employees are not showing up ? What Am I doing wrong here ?
You only create one HashMap instance
HashMap<String, String> keyValuePair = new HashMap<String, String>();
Then you keep adding this same instance to the list. That why all the maps in the list are the same instance.
You have to call keyValuePair = new HashMap<String, String>();
before each time you initialize a new map to be added to the list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.