スタッフの特定の指定コンタイオンリストがあるxmlを解析しています...したがって、解析中にハッシュマップを使用して値を保存します。配列リストにstaffdataを継続的に追加し、終了要素タグが見つかったら、その配列リストオブジェクトをHashMapに入れ、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
{
}
}
上記のコードのコメント行で、ARRAYLIST に DAA が含まれているかどうかを確認すると、データが表示されますが、ハッシュマップに保存されていない理由がわかりません.....
解決策を教えてください...
前もって感謝します