Read xml with XStream

0

I have an exercise on reading xml in java. My teacher indicated using the XStream library and I can not. I read about the documentation but could not identify the problem.  I have Transaction class with all variables and its gets and sets. I use the same class to do with a json using Gson and it works fine. or 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());


    }

You're experiencing 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>......
    
asked by anonymous 12.10.2018 / 20:54

1 answer

1

Use FileReader to read your file and pass it as a parameter in fromXML(fileReader) method.

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());


}
    
21.10.2018 / 22:35