我正在解析 xml,其中包含特定的指定人员列表... 因此,在解析时,我使用 hashmap 来存储值。我不断在 arraylist 中添加工作人员数据,当我找到结束元素标记时,我将该 arraylist 对象放入 HashMap 中,并清除 Arraylist 以存储下一个数据。
但是当我打印 hashmap 时,我发现 hashmap 中只有键,根本没有数组列表......
如果我在将 arraylist 放入 hashmap 后不清除 arraylist,则在 hashmap 中找到值...但所有值都聚集在一起..所以我必须在将 arraylist 放入 hash map 后清除 arraylist...
**HERE IS MY CODE OF XML PARSING**
package com.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class StaffXmlHandler extends DefaultHandler
{
static int totalSection=0;
static HashMap<String,ArrayList<Staff>> staffListWithDesignation=new HashMap<String,ArrayList<Staff>>();
ArrayList<Staff> staffList=new ArrayList<Staff>();
String designationName;
public static HashMap<String,ArrayList<Staff>> getStaffListWithDesignation()
{
return staffListWithDesignation;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
if(localName.equals("designation"))
{
designationName= attributes.getValue("name");
System.out.println("=====================>>>>Designation="+designationName);
}
else if (localName.equals("staff"))
{
//System.out.println("======>>>>fetching data from staff");
String employeeName = attributes.getValue("name");
String employeeEmail=attributes.getValue("email");
String employeePost=attributes.getValue("designation");
String employeePhone=attributes.getValue("phone");
Staff s=new Staff();
s.setEmployeeName(employeeName);
s.setEmployeeEmail(employeeEmail);
s.setEmployeePhone(employeePhone);
s.setEmployeePost(employeePost);
staffList.add(s);
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
/** set value */
if (localName.equalsIgnoreCase("designation"))
{
// Staff[] s=new Staff[staffList.size()];
// System.out.println("=========================="+designationName+"=======================");
// for(int i=0;i<staffList.size();i++)
// {
// System.out.println("=====Record "+(i+1)+"=====");
// s[i]=new Staff();
// s[i]=staffList.get(i);
// System.out.println("Name:"+s[i].getEmployeeName());
// System.out.println("email:"+s[i].getEmployeeEmail());
//
//
// }
ArrayList<Staff> temp=staffList;
staffListWithDesignation.put(designationName, temp);
staffList.clear();
designationName=null;
}
}
public void endDocument ()
{
System.out.println("==================End Element tag");
Iterator<String> iterator =staffListWithDesignation.keySet().iterator();
while (iterator.hasNext())
{
String key = (String) iterator.next();
System.out.println("=====> " + key + "======" + StaffXmlHandler.staffListWithDesignation.get(key));
}
}
@Override
public void characters(char[] ch, int start, int length)throws SAXException
{
}
}
在上面的代码中,在注释行中,如果我检查数组列表是否包含 DAA,那么它会显示数据,但不知道为什么它不存储在 HASHMAP 中......
请给我一些解决方案...
提前致谢
最佳答案
将列表放入 map 后,创建一个新列表。当您将列表放入 map 时,它不会复制它,因此您对列表的进一步操作(添加更多内容或清除它)会发生在同一个列表上。
关于java - arraylist 未存储在 hashmap 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5521989/