PDA

View Full Version : [JAVA/XML] Problema XML serializer... stampa il risultato su schermo, ma non su file!


doctorAle85
07-11-2009, 10:15
Ciao a tutti,
ho un grande problema...magari sarà una cavolata, ma sto impazzendo!:muro:

ho questo metodo...

public void serialize(String file) throws IOException {
FileWriter fw;
fw=new FileWriter(file);
BufferedWriter bw;
bw=new BufferedWriter (fw);
OutputFormat format = new OutputFormat("XML",null,true);
format.setIndent(1);
format.setIndenting(true);
format.setDoctype(null,"DTD/example.dtd");
ser = new XMLSerializer(format);
ser.setOutputCharStream(bw);
ser.setOutputByteStream(System.out);
ser.serialize(document);

bw.flush();
bw.close();
}


...il problema è che ser.setOutputByteStream(System.out) mi stampa nella console il risultato esatto XML che dovrebbe mettermi nel file.
ser.setOutputCharStream(bw) invece non scrive su file!...cioè, mi crea il file ma è vuoto!!!

morskott
07-11-2009, 10:20
Ciao a tutti,
ho un grande problema...magari sarà una cavolata, ma sto impazzendo!:muro:

ho questo metodo...

public void serialize(String file) throws IOException {
FileWriter fw;
fw=new FileWriter(file);
BufferedWriter bw;
bw=new BufferedWriter (fw);
OutputFormat format = new OutputFormat("XML",null,true);
format.setIndent(1);
format.setIndenting(true);
format.setDoctype(null,"DTD/example.dtd");
ser = new XMLSerializer(format);
ser.setOutputCharStream(bw);
ser.setOutputByteStream(System.out);
ser.serialize(document);

bw.flush();
bw.close();
}


...il problema è che ser.setOutputByteStream(System.out) mi stampa nella console il risultato esatto XML che dovrebbe mettermi nel file.
ser.setOutputCharStream(bw) invece non scrive su file!...cioè, mi crea il file ma è vuoto!!!

se ti piacciono le puzzonatepublic void serialize(String file) throws IOException {
FileWriter fw;
fw=new FileWriter(file);

FileOutputStream fos=new FileOutputStream(file);
PrintStream ps=new PrintStream(fos);
PrintStream out=System.out;
System.setOut(ps);

BufferedWriter bw;
bw=new BufferedWriter (fw);
OutputFormat format = new OutputFormat("XML",null,true);
format.setIndent(1);
format.setIndenting(true);
format.setDoctype(null,"DTD/example.dtd");
ser = new XMLSerializer(format);
ser.setOutputCharStream(bw);
ser.setOutputByteStream(System.out);
ser.serialize(document);

bw.flush();
bw.close();
System.setOut(out);
}
Non so se funziona ed è una soluzione, per così dire, sporca!!!