2017-10-16 149 views
0

我需要根據特定列值 將錶轉儲爲多個xml文件。 表如何根據列文件將數據表分割成多個文件?

Progressive  ID Content 
1    A Test1 
2    A Test2 
3    A Test3 
4    B Test1 
5    B Test2 
6    B Test3 
7    C Test1 
8    C Test2 

我的問題是我將要輸出該表到基於ID列 我試圖寫一個算法來做到這一點的價值XML文件,但我沒有跟蹤的ID值 我04-0030-03代碼是

tmp_id = ""; 
while (resultset.next()) { 
    if(resultset.getString("ID")!=tmp_id){ 
     create new xml file 
     tmp_id = resultset.getString("ID"); 
    } 
    write the remain data corresponding to same id 
    *****if (current resultset.getString("ID")!= the next resultset.getString("ID")) 
    { 
     close the xml file 
    } 
} 

在我的主要問題是*****地步,如果電流id是從下一個ID或者沒有 請幫助我,糾正我的算法 的出應該不同,我不能籤像那樣

file1.xml should have 
1    A Test1 
2    A Test2 
3    A Test3 


file2.xml should have 
4    B Test1 
5    B Test2 
6    B Test3 

file3.xml should have 
7    C Test1 
8    C Test2 

更新原來​​的職位,以添加當前的算法我現在使用,使其更容易理解我的問題

int tmp_blockid = 0; 
boolean flagIsOpen = false; 
while (rs.next()) { 

    //Check if the IDBLOCK is different from the tmp_blockid if yes write new file 
    if (rs.getInt("FEIDBLC") != tmp_blockid) {      
     flagIsOpen = true; 
     outputFactory = XMLOutputFactory.newInstance(); 
     fileOutputStream = new 
     FileOutputStream(newFile("C:\\Users\\test\\Desktop\\Projects\\"+ "new.xml")); 
     writer = outputFactory.createXMLStreamWriter(fileOutputStream); 

     //Method to write the header of the xml 
     writeHeader(progrS, rs, writer); 
     System.out.println("Start"); 
     tmp_blockid = rs.getInt("FEIDBLC"); 
    } 

    //Method to write the body of the xml 
    writeBody(rs, writer); 

    //This is where is my problem this condition should check if the 
    //current id different from the next id on the next resultset row but i failed here 
    if (rs.getInt("FEIDBLC") != tmp_blockid) { 
     flagIsOpen = false; 

     //Method to write the footer of the xml 
     writeFooter(writer); 
     if (fileOutputStream != null) { 
      fileOutputStream.close(); 
     } 
     System.out.println("End"); 
    } 
} 
//to close the file in case if the resultset have some rows 
    if (flagIsOpen) { 
     flagIsOpen = false; 
     writeFooter(writer); 
     if (fileOutputStream != null) { 
      fileOutputStream.close(); 
     }     
    } 

預先感謝您

回答

0

使用一個變量(如Writer)寫入XML文件。在創建新的之前直接關閉它。循環之後需要更接近一個。請確保您的查詢按ID列排序。

tmp_id = ""; 
Writer out = null; 
while (resultset.next()) { 
    if(resultset.getString("ID")!=tmp_id){ 
     if(out != null) { 
      out.close(); 
     } 
     //create new xml file 
     out = new FileWriter(...); 
     tmp_id = resultset.getString("ID"); 
    } 
    write the remain data corresponding to same id 
} 
if(out != null) { 
    out.close(); 
} 
+0

我與我的要求結合你的答案ID和我得到正確的答案這幫助我解決問題 –

0

正確答案感謝@vanje是

while (rs.next()) { 

    //Check if the IDBLOCK is different from the tmp_blockid if yes write new file 
    if (rs.getInt("FEIDBLC") != tmp_blockid) { 
    //to close the file in case if the resultset have some rows 
    if (flagIsOpen) { 
     flagIsOpen = false; 
     writeFooter(writer); 
     if (fileOutputStream != null) { 
      fileOutputStream.close(); 
     }     
    } 
     flagIsOpen = true; 
     outputFactory = XMLOutputFactory.newInstance(); 
     fileOutputStream = new 
     FileOutputStream(newFile("C:\\Users\\test\\Desktop\\Projects\\"+ "new.xml")); 
     writer = outputFactory.createXMLStreamWriter(fileOutputStream); 

     //Method to write the header of the xml 
     writeHeader(progrS, rs, writer); 
     System.out.println("Start"); 
     tmp_blockid = rs.getInt("FEIDBLC"); 
    } 

    //Method to write the body of the xml 
    writeBody(rs, writer); 


} 
//to close the file in case if the resultset have some rows 
    if (flagIsOpen) { 
     flagIsOpen = false; 
     writeFooter(writer); 
     if (fileOutputStream != null) { 
      fileOutputStream.close(); 
     }     
    } 
相關問題