Salvando as alterações de um arquivo xml em disco por DOM em java

1 resposta
S

Olá, alguem tem algum exemplo de como alterar os dados de um arquivo xml usando Dom em Java de forma que os dados fiquem armazenados em disco e não na memória?

1 Resposta

S

Para quem tiver esta duvida consegui a resposta.
deve transformar o documento xml em um arquivo file:

public void writeXmlFile() {
	try {
		// Prepare the DOM document for writing
		Source source = new DOMSource(document);

		// Prepare the output file
		File file = new File(xml);
		Result result = new StreamResult(file);

		// Write the DOM document to the file
		Transformer xformer = TransformerFactory.newInstance()
				.newTransformer();
		xformer.transform(source, result);
		System.out.println("ok - alterado com sucesso");
		 
	} catch (TransformerConfigurationException tce) {
		 System.out.println ("\n** Transformer Factory error");
		   System.out.println("   " + tce.getMessage() );

		   // Use the contained exception, if any
		   Throwable x = tce;
		   if (tce.getException() != null)
		       x = tce.getException();
		   x.printStackTrace();

	} catch (TransformerException te) {
//			 Error generated by the parser

System.out.println ("\n** Transformation error");

System.out.println("   " + te.getMessage() );
// Use the contained exception, if any
		   Throwable x = te;
		   if (te.getException() != null)
		       x = te.getException();
		   x.printStackTrace();
Criado 17 de agosto de 2005
Ultima resposta 22 de ago. de 2005
Respostas 1
Participantes 1