2014-10-19 55 views
0

我在将数据存储在从本地xml文件解析的Hashmap的Arraylist中时遇到问题。在数组列表中存储Sax解析器数据为HashMap

首先,这里是我的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> 

我要存储的Hashmap每对titlelinkdate然后把它里面的HashMap数组列表。

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

这里是我的声明HashMap和包含HashMap的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解析器endElement方法,我检查,如果元素是titledatelink,然后把它的数据一个HashMap中,并最终将其添加内部数组列表,但数据中的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中仅添加了最后一名员工详细信息多次?以及为什么前两名员工的员工细节没有显示出来? 我在这里做错了什么?

+0

http://simple.sourceforge.net/ – 2014-10-19 13:36:10

回答

2

你只创建一个HashMap实例

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

然后你不断增加的同一实例到列表中。这就是为什么列表中的所有地图都是同一个实例。

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

+0

谢谢你的作品,但有一个新问题。不是像这样添加它,{date = hs2,title = Harry2,link = Smith2},它将每个键/值对添加到新的大括号中,[{title = Harry0},{link = Smith0},{ date = hs0},{title = Harry1},{link = Smith1},{date = hs1},{title = Harry2},{link = Smith2},{date = hs2}] – 2014-10-19 13:31:18

+0

@Riley Willow你应该创建一个新的HashMap放在正确的位置,这是在分析新员工的XML之前。 – Eran 2014-10-19 14:20:05