Xml

8 respostas
fabiodurgante
public  void inserirFILE(Cliente cli) throws excessao {
       
              Cliente cliente = new Cliente();   
      

            cliente.setCpf_Cliente(cli.getCpf_Cliente());
      
       cliente.setid_Cliente(cli.getid_Cliente());      
       cliente.setEstado_Cliente(cli.getEstado_Cliente());       
    cliente.setNome_Cliente(cli.getNome_Cliente());      
       
           
        try {     
            
              OutputStream outputStream = new FileOutputStream("Arquivo.xml"); 
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); 
XMLEncoder encoder = new XMLEncoder(bufferedOutputStream); 

encoder.writeObject(cliente); 


encoder.close(); 
bufferedOutputStream.close(); 
outputStream.close(); 
        } catch (IOException e) {     
        }     
  
    
    }

seu resultado

<?xml version="1.0" encoding="UTF-8" ?>
  • [CPF removido]
  • RJ
  • juninho

so que so grava 1 unico registro os outros sao gravador por cima desse quero fazer gravar um abaixo do outro alguem tem alguma dica ???

8 Respostas

Pedrosa

Use o xtream para manipular objetos para xml e vice-versa:
http://xstream.codehaus.org/tutorial.html

fabiodurgante

funciono aqui perfeito XML

public  void Serialize(Cliente cli)  {
       
        XStream Serialize = new XStream();

        try {
            FileOutputStream fs = new FileOutputStream("Cliente.xml");
            Serialize.toXML(cli, fs);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
  
        }
   

 public  void DesSerialize(Cliente cli) throws FileNotFoundException, IOException  { 
 
     
   XStream DesSerialize = new XStream(new DomDriver());
        Cliente e = new Cliente();

        try {
            FileInputStream fis = new FileInputStream("Cliente.xml");
            DesSerialize.fromXML(fis, e);

            //print the data from the object that has been read
            System.out.println(e.toString());

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }

seu resultado

0 [CPF removido] FABIO 1111-11-11 00:00:00.0 BRT RS

presisa do xstream-1.3.1.jar na biblioteca

e uma coisa ele so faz de um unico cliente quando tento fazer de outro ele faz por cima do que ja estava quero que faça abaixo do anterior no mesmo arquivo alguem tem alguma ideia ???

peczenyj

Ora. vc esta mandando o arquivo.xml ser sobreescrito a cada vez que vc quiser escrever no mesmo.

Se vc quer ter uma lista de clientes vc pode fazer duas coisas:

  • abrir o arquivo.xml no modo append (passe true como segundo parametro do contrutor do FileOutputStream
  • serializar um array ou lista de clientes.
fabiodurgante

FileOutputStream fs = new FileOutputStream(“Cliente.xml”,true);

assim da erro cara nao sei o porque
[Fatal Error] :7:12: The markup in the document following the root element must be well-formed.

:12: The markup in the document following the root element must be well-formed.

com.thoughtworks.xstream.io.StreamException:  : The markup in the document following the root element must be well-formed.

at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:86)

at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:70)

at com.thoughtworks.xstream.XStream.fromXML(XStream.java:891)

at ClienteDao.DesSerialize(ClienteDao.java:69)
fabiodurgante

quando eu uso a funcao

public  void DesSerialize() throws FileNotFoundException, IOException  {
      
 XStream DesSerialize = new XStream(new DomDriver());
        Cliente cli = new Cliente();

        try {
            FileInputStream fis = new FileInputStream("Cliente.xml");
            DesSerialize.fromXML(fis, cli);

            //print the data from the object that has been read
            System.out.println(cli.toString());

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
}

ela me retorna

Cliente@1ef9157

e teria que retorna id nome cpf
alguem sabe porque isso ???

M

Ele retorna um objeto do tipo Cliente. Tente fazer isso:

// após carregar o objeto:
System.out.prinln(cli.getId());
System.out.prinln(cli.getNome());
System.out.prinln(cli.getCpf());
// esses getters são supostos. Podem ser apresentados de forma diferente
fabiodurgante

exatamente isso entendi o esquema ele carrega tudo mas em CLIENTE e nao é possivel larga direto o cliente
tem que ser um por 1 ok valeu cara

fabiodurgante

ultima coisa

public  void Serialize(Cliente cli)  {
 XStream xs = new XStream();

     
        try {
            FileOutputStream fs = new FileOutputStream("Cliente.xml",true);
            xs.toXML(cli, fs);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

  
        }

quando eu coloco true a primeira vez da certo depois quando ele vai escrever e nao sobrescrever ocorre esse erro ai

sem o true funciona so que fica sempre sobrescrevendo e eu nao quero isso quero poder ter 4 5 6 assim por diante cliente no XML

Fatal Error] :7:12: The markup in the document following the root element must be well-formed.

com.thoughtworks.xstream.io.StreamException:  : The markup in the document following the root element must be well-formed.

at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:86)

at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:70)

at com.thoughtworks.xstream.XStream.fromXML(XStream.java:891)
Criado 2 de fevereiro de 2009
Ultima resposta 2 de fev. de 2009
Respostas 8
Participantes 4