How to save the variables to be used in the next .Java?

0

My main (parser) code looks like this:

public class pFormasDePagamento {

public static void parseXML(String xml)
        throws ParserConfigurationException, SAXException, IOException {
    InputSource is = new InputSource(new StringReader(xml));
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(is);
    doc.getDocumentElement().normalize();
    String valFormaOutros = doc.getElementsByTagName("VALOR").item(0)
            .getTextContent();
    String descForma = doc.getElementsByTagName("DESCRICAO").item(0)
            .getTextContent();
    String valForma = doc.getElementsByTagName("VALOR").item(1)
            .getTextContent();
    String descForma1 = doc.getElementsByTagName("DESCRICAO").item(1)
            .getTextContent();
    String valForma1 = doc.getElementsByTagName("VALOR").item(2)
            .getTextContent();
    String descForma2 = doc.getElementsByTagName("DESCRICAO").item(2)
            .getTextContent();
    String valForma2 = doc.getElementsByTagName("VALOR").item(3)
            .getTextContent();
    String descForma3 = doc.getElementsByTagName("DESCRICAO").item(3)
            .getTextContent();
    String valForma3 = doc.getElementsByTagName("VALOR").item(4)
            .getTextContent();
    String descForma4 = doc.getElementsByTagName("DESCRICAO").item(4)
            .getTextContent();
    String valForma4 = doc.getElementsByTagName("VALOR").item(5)
            .getTextContent();
    String descForma5 = doc.getElementsByTagName("DESCRICAO").item(5)
            .getTextContent();
    String valForma5 = doc.getElementsByTagName("VALOR").item(6)
            .getTextContent();
}}

I would like to save all of these strings (valForma1 ... 5) to be reused in other .java

How can I resolve this?

    
asked by anonymous 01.10.2014 / 15:02

2 answers

2

Create a class that contains these properties and return an instance (in an array, for example) of that class.

Type like this:

public class FormaDePagamento {
    public String desc;
    public String valor;
}

public class pFormasDePagamento {

    public static FormaDePagamento[] parseXML(String xml)
            throws ParserConfigurationException, SAXException, IOException {
        InputSource is = new InputSource(new StringReader(xml));
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(is);
        doc.getDocumentElement().normalize();

        FormaDePagamento[] formas = new FormaDePagamento[5];
        for(int n = 0; n < 5; n++) {
            formas[n] = new FormaDePagamento();
            formas[n].desc = doc.getElementsByTagName("DESCRICAO").item(n).getTextContent();
            formas[n].valor = doc.getElementsByTagName("VALOR").item(n).getTextContent();
        }

        return formas;
    }
}

/ * in: "forms [n] .val = doc.getElementsByTagName" would not be: "forms [n] .valor = doc.getElementsByTagName"? * /

    
01.10.2014 / 16:43
0

Renan, you can use the native Sqlite or SharedPreferences database. In the case of SharedPreferences it is very simple, and can be used in your Activity in this way:

public static final String KEY = "nome_chave";
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
/*Para armazenar um valor*/
sharedPreferences.edit().putString(KEY , "valor").commit();
/*Para ler um valor armazenado*/
sharedPreferences.getString(KEY , "valor padrão caso não tenha valor para a chave");
    
02.10.2014 / 17:09