Olá,
Estou tendo problemas pra gravar em um arquivo XML, até que consigo gerar o arquivo, mas ele sempre apaga o que tinha antes pra salvar o atual. Alguma idéia de como posso resolver essa pendenga. Segue o código da classe que estou usando:
public static void gerarXml(Local local) throws FileNotFoundException {
XStream xStream = new XStream();
//Usamos o método que detecta as classes anotadas
xStream.autodetectAnnotations(true);
String documento = xStream.toXML(local);
salvarArquivo(xStream.toXML(local), "localXml.xml");
}
private static void lerXmlAnnotation() {
FileReader reader = null;
try {
//carrega o arquivo XML para um objeto reader
reader = new FileReader(
"C:\\DengueXMLArquivos\\localXml.xml"
);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//Cria o objeto xstream
XStream xStream = new XStream(new DomDriver());
//Para ler vamos utilizar outro método o
//processAnnotations() e como parâmetro passamos
//a classe que possui as anotações
xStream.processAnnotations(Local.class);
Local local = (Local) xStream.fromXML(reader);
System.out.println(local.toString());
}
private static void salvarArquivo(String documento, String file) {
File path = new File("C:\\DengueXMLArquivos\\" + file);
try {
PrintWriter writer = new PrintWriter(path);
writer.println(
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?>"
);
writer.println(documento);
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}