将Array解析器中的Sax解析器数据存储为Hashmap

时间:2014-10-19 13:12:41

标签: java android arrays date arraylist

我无法在Hashmap的Arraylist中存储数据,该数据是从本地xml文件中解析的。

首先,这是我的xml文件,

<?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>

我想将每对titlelinkdate存储在Hashmap中,然后将其放在Hashmap的数组列表中。

我正在使用SAX解析器,但我无法实现我想要做的事情,

这是我的代码,用于声明Hashmap和Hashmaps的ArrayList,

ArrayList<HashMap<String, String>> xml_Array = new ArrayList<HashMap<String,String>>();

    HashMap<String, String> keyValuePair = new HashMap<String, String>();

现在我做的是,

@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 + "");
    }

在Sax Parser的endElement方法中,我检查了元素是titledate还是link,然后将其数据放入散列图并最终添加在数组列表中,但数据在arraylist中被覆盖而不是追加,

这是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}]

在arraylist中多次添加的最后一个员工详细信息是什么?为什么前两名员工的员工细节没有出现? 我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

您只创建一个HashMap实例

HashMap<String, String> keyValuePair = new HashMap<String, String>();

然后,您继续将同一个实例添加到列表中。这就是为什么列表中的所有地图都是同一个实例。

每次初始化要添加到列表中的新地图之前,您都必须致电keyValuePair = new HashMap<String, String>();

相关问题