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

我们如何将所有值存储在 Map 中?

4

4 回答 4

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

You are keeping keys same everytime, as "Item No" etc, so everytime, values will be overwritten. Put map into a list and finally send that list to your method.

于 2013-10-16T12:40:28.490 回答
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");
} 
于 2013-10-16T12:42:57.423 回答
0

您需要将值列表传递给 generateCSVfile()。现在,你只经过一排。

一种方法是将所有地图放在一个列表中,然后将该列表传递给 generateCSVfile。

但我建议您创建一个新类来保存这些值。说一个名为Row它将保存您的值的类,并将 List 传递给 generateCSVfile()

于 2013-10-16T12:46:05.680 回答
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){

        }
    }
于 2013-10-16T12:47:13.850 回答