0

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 ?

1

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it works, but there is a new issue. Instead of adding it like this, {date=hs2, title=Harry2, link=Smith2} , it is adding each key/value pair in new curly braces like this, [{title=Harry0}, {link=Smith0}, {date=hs0}, {title=Harry1}, {link=Smith1}, {date=hs1}, {title=Harry2}, {link=Smith2}, {date=hs2}]
@Riley Willow you should create a new HashMap in the right place, which is before parsing the xml of a new employee.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.