Read xml with Xstream

Asked

Viewed 159 times

0

I have a java xml reading exercise. My teacher indicated using the Xstream library and I’m not getting it. I read about the documentation, but I can’t identify the problem. I have the Transaction class with all variables and their gets and sets. I use the same class to do with a json using Gson and it works normally. the Main:

{

import java.io.File;

import com.thoughtworks.xstream.XStream;


    public static void main(String[] args){


        File remessajson = new File("C:remessa.xml");
        System.out.println(remessajson.exists());
        XStream xs = new XStream();

        String xml = xs.toXML(remessajson);     
        Transacao a = (Transacao)xs.fromXML(xml);


        System.out.println(a.getBancoPag());


    }

Is presenting the error:

Security framework of XStream not initialized, XStream is probably vulnerable.
Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast to pack.Transacao
  at pack.Main.main(Main.java:19)
true

The first xml items:

<list>
<br.com.pageseguro.RemessaCartaoCredito>
<nome>Adelaide Carvalhaes</nome>
<CPF>56608514522</CPF>
<bancoRecebimento>Caixa Econômica</bancoRecebimento>
<bancoPagamento>Banco Safra</bancoPagamento>
<data>2018-09-02 02:58:10.96 UTC</data>
<valor>362.4101749037519</valor>
<numeroCartao>98315792</numeroCartao>
<nomeTitular>LIEDSON LAGO</nomeTitular>
<parcelas>2</parcelas>
</br.com.pageseguro.RemessaCartaoCredito>
<br.com.pageseguro.RemessaCartaoCredito>
<nome>Viriato Ayres</nome>
<CPF>41057727598</CPF>
<bancoRecebimento>Banco do Brasil</bancoRecebimento>
<bancoPagamento>Banco do Brasil</bancoPagamento>
<data>2018-09-02 02:58:10.96 UTC</data>
<valor>475.60665046855246</valor>
<numeroCartao>92579840</numeroCartao>
<nomeTitular>VIRIATO AYRES</nomeTitular>
<parcelas>6</parcelas>
</br.com.pageseguro.RemessaCartaoCredito>......

1 answer

1


Use Filereader to read your file and pass as parameter in the method fromXML(fileReader).

public static void main(String[] args){

    FileReader reader = new FileReader(new File("C:\\remessa.xml"));
    System.out.println(remessajson.exists());
    XStream xs = new XStream();

    Transacao a = (Transacao)xs.fromXML(reader);


    System.out.println(a.getBancoPag());


}
  • Man, sorry it took so long. But it worked that way anyway. I had done another one, but with your suggestion it got easier. Thanks bro.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.