Created
June 26, 2016 22:20
-
-
Save drmcarvalho/ff8f78de2d2ff77c47416a5af03094cf to your computer and use it in GitHub Desktop.
Exemplo de como adicionar dados em um arquivo XML.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package expxml; | |
import java.io.ByteArrayInputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.transform.Result; | |
import javax.xml.transform.Source; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerConfigurationException; | |
import javax.xml.transform.TransformerException; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.dom.DOMSource; | |
import javax.xml.transform.stream.StreamResult; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.xml.sax.SAXException; | |
/** | |
* @author Dener | |
*/ | |
public class Aluno { | |
public Aluno(String nome, String curso) { | |
this.nome = nome; | |
this.curso = curso; | |
} | |
public Aluno() { | |
} | |
private String nome; | |
private String curso; | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public String getCurso() { | |
return curso; | |
} | |
public void setCurso(String curso) { | |
this.curso = curso; | |
} | |
//CRUD | |
public void addAluno(Aluno aluno, File arquivo) throws | |
ParserConfigurationException, | |
UnsupportedEncodingException, | |
SAXException, | |
IOException, | |
TransformerConfigurationException, | |
TransformerException { | |
if (!arquivo.exists()) { | |
//Cria o xml | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
StringBuilder xmlStringBuilder = new StringBuilder(); | |
xmlStringBuilder.append("<?xml version=").append("'1.0'").append("?>"). | |
append("<aluno>"). | |
append(" <nome>").append(aluno.getNome()).append("</nome>"). | |
append(" <curso>").append(aluno.getCurso()).append("</curso>"). | |
append("</aluno>"); | |
ByteArrayInputStream inPutByte = new ByteArrayInputStream(xmlStringBuilder.toString().getBytes("UTF-8")); | |
Document document = builder.parse(inPutByte); | |
document.getDocumentElement().normalize(); | |
/*Cria o arquivo XML com base no objeto document.*/ | |
Transformer transformer = TransformerFactory.newInstance().newTransformer(); | |
Result output = new StreamResult(arquivo); | |
Source in = new DOMSource(document); | |
transformer.transform(in, output); | |
} | |
else { | |
//Rescreve o XML | |
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | |
Document document = documentBuilder.parse(arquivo); | |
Element root = document.getDocumentElement(); | |
Collection<Aluno> alunos = new ArrayList<>(); | |
alunos.add(aluno); | |
for (Aluno a : alunos) { | |
Element novoAluno = document.createElement("aluno"); | |
Element nome = document.createElement("nome"); | |
nome.appendChild(document.createTextNode(a.getNome())); | |
novoAluno.appendChild(nome); | |
Element curso = document.createElement("curso"); | |
curso.appendChild(document.createTextNode(a.getCurso())); | |
novoAluno.appendChild(curso); | |
root.appendChild(novoAluno); | |
} | |
DOMSource source = new DOMSource(document); | |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); | |
Transformer transformer = transformerFactory.newTransformer(); | |
StreamResult result = new StreamResult(arquivo); | |
transformer.transform(source, result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment