Olá Pessoal, a classe para gerarXML já ta quase lá !
Eu coloquei nela uma função para zipar o xml tb, mas percebi um problema… Mando ele zipar o arquivo c:\xml\123.xml por exemplo e depois me devolver o zip dele, assim:
return arquivoRetorno = this.zipar(arquivo,nomeAleatorio,path);
até ai tudo bem, ele cria o 123.zip em c:\xml\ com 123.xml dentro.
Porém e aí o problema, ele cria uma estrutura de pastas “dentro do zip”, no WinZip não da pra perceber muito…, mas outros zip como o Rar você precisar entrar nas pastas e no Zip do XP ele fica vazio.
Segue abaixo a classe GeradorXML, o método que se deve usar mesmo é o gerarXML(String sql, Connection con, String path):
Desde já, grato
/*
* Created on Dec 21, 2006
* Jesus Te ama !
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package controle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author Celso Araujo
* @version 0.02
*/
public class GeradorXML {
public static Random aleatorio;
public GeradorXML(){};
public String gerarXML(String sql, Connection con, String path){
String arquivoRetorno= new String();
StringBuffer sb = new StringBuffer();
ResultSet rs;
String nomeAleatorio;
try {
Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql);
DatabaseMetaData dbmd = con.getMetaData(); //Este objeto contém informações sobre o bd
ResultSetMetaData rsmd = rs.getMetaData(); //pegar os metadados (colunas e tal).
int numCols = rsmd.getColumnCount(); //Pegando o números de colunas
nomeAleatorio = new String(this.gerarNome()); // Nome aleatório criado pelo método
File arquivo = new File(path+nomeAleatorio+".xml"); //cria o arquivo xml com nome aleatório
arquivoRetorno = arquivo.toString();
FileWriter writer = new FileWriter(arquivo,false);
PrintWriter saida = new PrintWriter(writer,false);
sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> \r\n");
sb.append("<dataroot xmlns:od=\"urn:schemas-microsoft-com:officedata\"> \r\n");
while(rs.next()) {
sb.append("<bampetro> \r\n");
for (int i=1; i<numCols; i++) {
sb.append("<"+rsmd.getColumnLabel(i)+">"+
rs.getObject(i).toString() +"</"+rsmd.getColumnLabel(i)+
">\r\n");
}
//rs.close();
//stmt.close();
sb.append("</bampetro> \r\n");
// System.out.println(sb.toString());
}
sb.append("</dataroot>");
saida.print(sb.toString());
saida.close();
writer.close();
arquivoRetorno = this.zipar(arquivo,nomeAleatorio,path);
}catch(IOException e)
{
e.getMessage();
}
catch(SQLException sqle)
{
sqle.getMessage();
}
return arquivoRetorno;
}
public String gerarNome() {
Random aleatorio = new Random(System.currentTimeMillis());
//System.out.println(aleatorio);
String sub,retorno;
sub=String.valueOf(aleatorio.nextGaussian());
retorno=sub.substring(sub.lastIndexOf(".")+1,sub.length());
return String.valueOf(retorno);
}
public String zipar(File xml, String nomeAleatorio, String path){
String nomeZip = new String();
try{
// Open the Zip
nomeZip = new String(path+nomeAleatorio+".zip");
FileOutputStream fos = new FileOutputStream (nomeZip);
ZipOutputStream zip = new ZipOutputStream (fos);
zip.setLevel(9);
zip.setMethod( ZipOutputStream.DEFLATED );
// get the element file we are going to add, using slashes in name.
String elementName = xml.toString();
// create the entry
ZipEntry entry = new ZipEntry(elementName);
entry.setTime(xml.lastModified());
// read contents of file we are going to put in the zip
int fileLength = (int)xml.length();
FileInputStream fis = new FileInputStream (xml);
byte[] wholeFile = new byte [fileLength];
int bytesRead = fis.read( wholeFile , 0 /* offset */ , fileLength );
// checking bytesRead not shown.
fis.close();
// no need to setCRC, or setSize as they are computed automatically.
zip.putNextEntry(entry);
// write the contents into the zip element
zip.write(wholeFile, 0, fileLength);
zip.setComment("Jesus te ama ");
zip.closeEntry();
// close the entire zip
zip.close();
}catch(Exception e)
{};
return nomeZip;
}
}