2017-10-16 6 views
0

Je dois vider une table dans plusieurs fichiers XML en fonction de la valeur de la colonne spécifique Par exemple. Tableaucomment diviser le tableau de données en plusieurs fichiers basés sur le fichier de colonne?

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 

ma question est que je suis va sortie ce tableau dans des fichiers XML basés sur la valeur de la colonne ID i essayer d'écrire un algorithme pour le faire, mais je ne ai pas suivi de la valeur ID mon code pesudo est

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 
    } 
} 

le problème principal est sur ***** point où je ne peux pas vérifier si l'identifiant courant est différent du prochain id ou non s'il vous plaît me aider et corriger mon algorithme l'out devrait être comme ça

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 

Mise à jour du message d'origine pour ajouter l'algorithme actuel j'utilise maintenant pour le rendre plus facile à comprendre ma question

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

vous remercie à l'avance

Répondre

0

Utilisez une variable (comme un Writer) pour écrire dans le fichier XML. Fermez-le directement avant de créer le nouveau. Après la boucle, un plus proche est nécessaire. Assurez-vous que votre requête commande l'enregistrement par colonne 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

i mettiez id réponse avec mon exigence et j'obtenir la bonne réponse qui m'a aidé à résoudre le problème –

0

la bonne réponse grâce à @vanje est

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