title: Java & XML: CRUD
body: <p>I would like to know how to manipulate XML files in Java.</p>
  
  <p>I know how to do the create and read, I am just missing how to do the update and delete them. If anyone knows a website with tutorials, about it I would appreciate it. I also would like to know how to manipulate the records. </p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>/*
   * Classe de Lista de Compras
   */
  package xml01;
  
  public class ListaCompras {
      public String descricao;
      public int quant;
      public double valor;
  }
  
  package xml01;
  
  import java.io.File;
  import java.io.FileWriter;
  import java.util.Scanner;
  
  public class GravarLer {
  
      static void gravar(String xml) {
          try {
              FileWriter w = new FileWriter("teste.xml");
              w.write(xml);
              w.close();
          } catch (Exception e) {
              System.out.println("Erro ao gravar XML: " + e);
          }
      }
  
      static String ler() {
          try {
              Scanner in = new Scanner(new File("testeler.xml"));
              StringBuilder sb = new StringBuilder();
              while (in.hasNext()) {
                  sb.append(in.next());
              }
              in.close();
              return sb.toString();
          } catch (Exception e) {
              System.out.println("Erro ao ler XML: " + e);
          }
          return "";
      }
  }
  
  
  /*
   * XStream
   */
  package xml01;
  
  import com.thoughtworks.xstream.XStream;
  
  public class Xml01 {
  
      public static void main(String[] args) {
          // Cria um vetor de registros ListaCompras
          ListaCompras lista[] = new ListaCompras[10];
          // Inicializa os registros com numeros aleatorios
          for (int i = 0; i &lt; lista.length; i++) {
              lista[i] = new ListaCompras();
              lista[i].quant = i + 2000 * i;
              lista[i].valor = i + 0.5 * 11 * i;
          }
  
          lista[0].descricao = "Arroz";
          lista[1].descricao = "Feijao";
          lista[2].descricao = "Macarrao";
          lista[3].descricao = "Agua mineral";
          lista[4].descricao = "Leite";
          lista[5].descricao = "PÃ£o de forma";
          lista[6].descricao = "Manteiga";
          lista[7].descricao = "Banana";
          lista[8].descricao = "Laranja";
          lista[9].descricao = "MaÃ§Ã£";
  
  
          // Inicializa o XStream
          XStream xstream = new XStream();
          // Converte o objeto ListaCompras para XML
          String xml = xstream.toXML(lista);
          // Imprime na tela
          System.out.println(xml);
  
          // Chamando a classe GravarLer
          GravarLer ClasseGravar = new GravarLer();
          // Chamando o metodo
          GravarLer.gravar(xml);
          // GravarLer.ler();
          // System.out.println(GravarLer.ler());
      }
  }
  </code></pre>