2015-10-12 48 views
0

我一直在使用SAX解析器從Excel工作表讀取數據,但是我遇到了問題。我需要保存前四行然後迭代其他行,但是當我將最初的行保存到不同的HashMap時,這些行會被覆蓋,並且我在所有結構中獲得相同的字段。使用sax解析器的endElement覆蓋HashMap變量

這是endElement方法的代碼:

public void endElement(String uri, String localName, String name) 
     throws SAXException { 

    String cellValue = null; 
    //String thisStr = null; 

    // v => contents of a cell 
    if ("v".equals(name)) { 
     // Process the value contents as required. 
     // Do now, as characters() may be called more than once 
     switch (nextDataType) { 

     case BOOL: 
      char first = value.charAt(0); 
      cellValue=first == '0' ? "false" : "true"; 
      break; 

     case ERROR: 
      cellValue=new String(value.toString()); 
      break; 

     case FORMULA: 
      // A formula could result in a string value, 
      // so always add double-quote characters. 
      cellValue=new String(value.toString()); 
      break; 

     case INLINESTR: 
      XSSFRichTextString rtsi = new XSSFRichTextString(value.toString()); 
      cellValue=new String(rtsi.toString()); 
      break; 

     case SSTINDEX: 
      String sstIndex = value.toString(); 
      try { 
       int idx = Integer.parseInt(sstIndex); 
       XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx)); 
       cellValue=new String(rtss.toString()); 
      } 
      catch (NumberFormatException ex) { 
       System.out.println("Failed to parse SST index '" + sstIndex + "': " + ex.toString()); 
      } 
      break; 

     case NUMBER: 
      String n = value.toString(); 
      if (this.formatString != null && n.length() > 0){ 
       cellValue = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex, this.formatString); 
      } 
      else{ 
       cellValue=new String(n); 
      } 
      break; 

     default: 
      cellValue=""; 
      break; 
     } 

     // Output after we've seen the string contents 
     // Emit commas for any fields that were missing on this row 
     if (lastColumnNumber == -1) { 
      lastColumnNumber = 0; 
     } 

     // Might be the empty string. 
     rowValues.put(cellCoordinate,cellValue); 

     // Update column 
     if (thisColumn > -1) 
      lastColumnNumber = thisColumn; 

    } else if ("row".equals(name)) { 
     // We're onto a new row 
     databaseServices.archiveAcquisition(rowValues,rowCount); 
     //Clear the structure used to store row data 
     rowValues.clear(); 
     rowCount++; 
     lastColumnNumber = -1; 
    } 
} 

archiveAcquisition方法:

@Override 
public void archiveAcquisition(HashMap<String,String> actualRowValues, int index) { 
    switch(index){ 
    case 1: 
     firstRowValues=actualRowValues; 
     break; 
    case 2: 
     secondRowValues=actualRowValues; 
     break; 
    case 3: 
     thirdRowValues=actualRowValues; 
     break; 
    default: 
     //CREATE ALL THE OBJECT TO STORE THE ACQUISITION 
     //Create shift object, if it already exists, it'll be updated 
     Shift shift=new Shift(actualRowValues.get(ExcelMappingCoordinate.shift.getCoordinate()+index)); 
     shiftServices.create(shift); 

我不已瞭解問題出在哪裏,SAX庫的隱藏行爲?我也試着用一個外部對象來存儲,但問題是一樣的。

+1

'databaseServices.archiveAcquisition(new HashMap <>(rowValues),rowCount);'應該這樣做。 –

+0

是的,它工作。非常感謝 – luca

回答

0

問題是重複添加了相同的HashMap對象(rowValues),並將其清除。

快速黑客將是使每一次地圖的副本:

databaseServices.archiveAcquisition(new HashMap<>(rowValues),rowCount); 

更好的開始了新的rowValues,也許是局部變量。