a forma q usei pra fazer o reverso do parser foi a seguinte, (eh o mesmo
codigo q o pessoal ae pediu privadamente) :)
factory = DocumentBuilderFactory.newInstance();
xmlFile = new
File(getClass().getResource("/algum_pacote/arquivo.xml").getFile());
try {
builder = factory.newDocumentBuilder();
tFactory = TransformerFactory.newInstance();
transformer = tFactory.newTransformer();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerConfigurationException tce) {
System.out.println("\n** Transformer Factory error");
System.out.println(" " + tce.getMessage());
Throwable x = tce;
if (tce.getException() != null)
x = tce.getException();
x.printStackTrace();
}
fis = new FileInputStream(xmlFile);
document = builder.parse(fis);
fis.close();
// a partir daki vc pode acessar o conteudo do xml pelo objeto
Document
...
// edite alguns nodes...
algum_node.setNodeValue("valor");
// agora vc joga o Document devolta pro arquivo.xml
FileOutputStream fos = null;
try {
fos = new FileOutputStream(xmlFile);
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(fos);
transformer.transform(source, result);
} catch (TransformerException te) {
System.out.println("\n** Transformation error in add:");
System.out.println(" " + te.getMessage());
Throwable x = te;
if (te.getException() != null)
x = te.getException();
x.printStackTrace();
} catch (Exception ex) {
System.out.println("exc exp" + ex);
ex.printStackTrace();
} finally {
try {
fos.close();
} catch(Exception e) {}
}
abra�os... ;)
________________________
Bruno Cesar Borges [miojo]
Programador Java Semi-Pleno
ICQ: 8584744
MSN: [EMAIL PROTECTED]
*RUMO* ao Sun Certified Programmer for the Java 2 Platform 1.4
----- Original Message -----
From: "Carlos Villela" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, November 14, 2002 2:59 PM
Subject: Re: RES: [enterprise-list] Como salvar XML no DOM
> Eis o meu snippet favorito pra fazer isso (caso haja algum erro nele ou
uma forma mais facil de se fazer, por
> favor avise!):
>
> Document doc =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
> Element root = doc.createElement("stuff");
> // ...
> doc.appendChild(root);
> doc.normalize();
> OutputFormat format = new OutputFormat(doc);
> format.setIndenting(true);
> format.setEncoding("ISO-8859-1");
> StringWriter stringOut = new StringWriter();
> XMLSerializer serial = new XMLSerializer(stringOut, format);
> serial.serialize(doc.getDocumentElement());
> String xmlAsString = stringOut.toString();
>
> On Thu, 14 Nov 2002 13:36:45 -0300, F�bio Calegari - Mult-e
<[EMAIL PROTECTED]> escreveu :
>
> > De: F�bio Calegari - Mult-e <[EMAIL PROTECTED]>
> > Data: Thu, 14 Nov 2002 13:36:45 -0300
> > Para: <[EMAIL PROTECTED]>
> > Assunto: RES: [enterprise-list] Como salvar XML no DOM
> >
> > Poderia me dizer como fez, tb preciso disto !!
> >
> > Valeu !!
> >
> > -----Mensagem original-----
> > De: Bruno Borges [mailto:bcbjava@;yahoo.com.br]
> > Enviada em: quinta-feira, 14 de novembro de 2002 10:23
> > Para: [EMAIL PROTECTED]; Bruno Borges
> > Assunto: Re: [enterprise-list] Como salvar XML no DOM
> >
> >
> > huummm... jah resolvi... ;)
> >
> > thankz mesmo assim... ;)
> > ________________________
> > Bruno Cesar Borges [miojo]
> > Programador Java Semi-Pleno
> > ICQ: 8584744
> > MSN: [EMAIL PROTECTED]
> > *RUMO* ao Sun Certified Programmer for the Java 2 Platform 1.4
> >
> >
> >
> > ----- Original Message -----
> > From: "Bruno Borges" <[EMAIL PROTECTED]>
> > To: <[EMAIL PROTECTED]>
> > Sent: Thursday, November 14, 2002 9:58 AM
> > Subject: [enterprise-list] Como salvar XML no DOM
> >
> >
> > > Bom, depois de ter o XML parseado para um Document, e alterar o valor
> > > de alguns Nodes... como eu salvo essas altera��es devolta para o
> > > arquivo xml? to utilizando a api padrao do j2ee 1.3
> > > ________________________ Bruno Cesar Borges [miojo]
> > > Programador Java Semi-Pleno
> > > ICQ: 8584744
> > > MSN: [EMAIL PROTECTED]
> > > *RUMO* ao Sun Certified Programmer for the Java 2 Platform 1.4
> > >
> > > ______________________________________________________________________
> > > _
> > > Yahoo! Encontros
> > > O lugar certo para encontrar a sua alma g�mea.
> > > http://br.encontros.yahoo.com/
> > >
> > > ---------------------------------------------------------------------
> > > Para cancelar a subscri��o, envie mensagem para:
> > [EMAIL PROTECTED]
> > > Para comandos adicionais, envie mensagem para:
> > [EMAIL PROTECTED]
> >
> > _______________________________________________________________________
> >
> > Yahoo! GeoCities
> >
> > Tudo para criar o seu site: ferramentas f�ceis de usar, espa�o de sobra
> > e acess�rios.
> >
> > http://br.geocities.yahoo.com/
> >
> >
> > ---------------------------------------------------------------------
> > Para cancelar a subscri��o, envie mensagem para:
> > [EMAIL PROTECTED]
> > Para comandos adicionais, envie mensagem para:
> > [EMAIL PROTECTED]
> >
> >
> >
> >
> > ---------------------------------------------------------------------
> > Para cancelar a subscri��o, envie mensagem para:
[EMAIL PROTECTED]
> > Para comandos adicionais, envie mensagem para:
[EMAIL PROTECTED]
> >
> >
> >
> >
>
> ---------------------------------------------------------------------
> Para cancelar a subscri��o, envie mensagem para:
[EMAIL PROTECTED]
> Para comandos adicionais, envie mensagem para:
[EMAIL PROTECTED]
_______________________________________________________________________
Yahoo! Encontros
O lugar certo para encontrar a sua alma g�mea.
http://br.encontros.yahoo.com/
---------------------------------------------------------------------
Para cancelar a subscri��o, envie mensagem para:
[EMAIL PROTECTED]
Para comandos adicionais, envie mensagem para: [EMAIL PROTECTED]