2013-10-16 54 views
1

我試圖顯示所有記錄,但它只顯示最後一條記錄,因爲它的值每次都被覆蓋。如何解決這個問題?如何獲取所有記錄使用地圖

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(); 
    } 

我們如何存儲在圖中的所有值?

回答

0
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()); 

您每次保存的密鑰都是「Item No」等,所以每次都會覆蓋值。將地圖放入列表中,最後將該列表發送到您的方法。

+0

謝謝可以回覆。請告訴我,我可以用什麼來代替地圖? – Aanshi

+0

看看番石榴的[Multimap](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Multimap.html) – Uwe

0

正如我所看到的Map鍵值表示列的值 - 數據。因此,您將所有放入相同的密鑰中。

我建議你創建基本類Line並使用List<Line>

class Line{ 
private String ItemNo = ""; 
private String Description = ""; 
private String price = ""; 
private String baseQty = ""; 
private String VarQty = ""; 

//getters/setters 
} 

... 

List<Line> lines = new ArrayList<Line>(); 

相反:

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()); 

,我們會寫:

Line line = new Line(); 
line.setItemNo(eElement.getElementsByTagName("item_no").item(0).getTextContent()); 
line.setDescription(eElement.getElementsByTagName("description").item(0).getTextContent()); 
... 
lines.add(line); 

generateCsvFile方法,我們只是寫類似:

FileWriter writer = new FileWriter(sFilename,true); 
writer.append("Item No, Description, .....");// add header 

,並在遍歷目錄運行添加如下行:

for(Line line : lines){ 
writer.append(line.getItemNo()).append(","); 
writer.append(line.getDescription()).append(","); 
... 
writer.append("\n"); 
} 
+0

感謝您的回覆。可以請寫下更多? – Aanshi

+0

它顯示錯誤就像無法實例化類型行 – Aanshi

+0

看到我的例子,我在哪裏定義類行 –

0

你需要的是傳遞一個值列表來生成CSVfile()。現在,你只傳遞一行。

一種方法是將所有地圖放入列表中,並將列表傳遞給generateCSVfile。

但我建議你創建一個新的類來保存這些值。說一個名爲類將持有你的價值觀,並通過列表來generateCSVfile()

0

你需要地圖的對象列表

List< Map<String, String>> list = new ArrayList<Map<String,String>>(); 

for (int temp = 0; temp < nList.getLength(); temp++) { 
    Map<String, String> map = new HashMap<String, String>(); 
    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()); 
      list.add(map);    
     } 
} 

generateCsvFile("E:\\testCSV.csv", list); 

現在您generateCSVfile()方法看起來像

private static void generateCsvFile(String sFilename, List< Map<String, String>> test) { 
     try { 
      FileWriter writer = new FileWriter(sFilename,true); 
      for (Map<String, String> map : test) {   

      Iterator iterator = map.entrySet().iterator(); 
      while (iterator.hasNext()) { 
       Map.Entry<String,String> mapEntry = (Map.Entry<String,String>) iterator.next(); 
       System.out.println("key: " + mapEntry.getKey() + ", value:" + mapEntry.getValue()); 
       writer.append(mapEntry.getKey()); 
       writer.append(mapEntry.getValue()); 
       writer.append(","); 
       iterator.remove(); 
      } 
      } 
      writer.flush(); 
      writer.close(); 
     }catch(Exception e){ 

     } 
    } 
+0

感謝您的答覆。但它的不工作.its顯示相同的o/p – Aanshi

+0

@Aanshi請確保您調用generateCsvFile(「E:\\ testCSV .csv「,列表); for循環之外。 – Prabhakaran

+0

現在它的工作,但它顯示鍵值對中的值。所以我對這個writer.append mapEntry.getValue());但顯示錯誤 – Aanshi