我試圖顯示所有記錄,但它只顯示最後一條記錄,因爲它的值每次都被覆蓋。如何解決這個問題?如何獲取所有記錄使用地圖
File fXmlFile = new File("d:/formdata.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("record");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Map<String, Object> map = new HashMap<String, Object>();
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
map.put("Item No", eElement.getElementsByTagName("item_no").item(0).getTextContent());
map.put("Description", eElement.getElementsByTagName("description").item(0).getTextContent());
map.put("price", eElement.getElementsByTagName("price").item(0).getTextContent());
map.put("base qty", eElement.getElementsByTagName("base_qty").item(0).getTextContent());
map.put("Var qty", eElement.getElementsByTagName("var_qty").item(0).getTextContent());
generateCsvFile("E:\\testCSV.csv", map);
}
}
當調用generateCSVfile()
private static void generateCsvFile(String sFilename, Map<String, Object> test) {
try {
FileWriter writer = new FileWriter(sFilename,true);
Iterator iterator = test.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry mapEntry = (Map.Entry) iterator.next();
System.out.println("key: " + mapEntry.getKey() + ", value:" + mapEntry.getValue());
writer.append(mapEntry.getKey() + " = " + mapEntry.getValue());
writer.append(",");
iterator.remove();
}
writer.flush();
writer.close();
}
我們如何存儲在圖中的所有值?
謝謝可以回覆。請告訴我,我可以用什麼來代替地圖? – Aanshi
看看番石榴的[Multimap](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html) – Uwe