Is it possible to instantiate an object more than once in a class?

Asked

Viewed 125 times

0

I have a problem. I have 3 For’s, one inside the other, Inside the second For I make the instance of my object and soon after I pass the data of an xml file I read to that object and in the end I add everything in a List<MeuObjeto>. But in the third FOR I needed to instantiate my object again, because when I pass the dice to the object it only stores the first element. Here is an excerpt from the code:

 for (int y = 0; y < tagdadosGuia.getLength(); y++) {

                NodeList tagBeneficiario0 = ((Element) tagdadosGuia.item(y)).getElementsByTagName("unimed:beneficiario");
                NodeList tagProcedimentos = ((Element) tagdadosGuia.item(y)).getElementsByTagName("unimed:procedimentos");
                NodeList tagProcedimentos1 = ((Element) tagProcedimentos.item(0)).getElementsByTagName("unimed:dadosProcedimento");
                NodeList tagProcedimento = ((Element) tagProcedimentos1.item(0)).getElementsByTagName("unimed:procedimento");

                Element elementoBeneficiariox = (Element) tagBeneficiario0.item(0);
                String name = pegaTag(elementoBeneficiariox, "unimed:nomeBeneficiario");
                String nome = null;

                for (int a = 0; a < tagProcedimentos1.getLength(); a++) {
                    UnimedLote contato = new UnimedLote();

                    contato.setNumLote(lote);
                    nome = name;
                    System.err.println("Nomes: " + nome);
                    contato.setNomeBeneficiario(nome);

                    Element elementoBeneficiario12 = (Element) tagProcedimentos1.item(a);
                    String tot = pegaTag(elementoBeneficiario12, "unimed:valorInformado");
                    if (tot
                            == null) {
                        tot = "0";
                    }
                    BigDecimal vt = new BigDecimal(tot).setScale(2);
                    contato.setValorTotalInformado(vt);

                    /*Beneficiarios*/
                    Element elementoBeneficiario0 = (Element) tagBeneficiario0.item(0);
                    beneficiariosLote.add(pegaTag(elementoBeneficiario0, "unimed:numeroCarteira"));
                    contato.setCodigo(pegaTag(elementoBeneficiario0, "unimed:numeroCarteira"));

                    /*Dados das Guias*/
                    Element elementoBeneficiario1 = (Element) tagdadosGuia.item(y);
                    beneficiariosLote.add(pegaTag(elementoBeneficiario1, "unimed:numeroGuiaOperadora"));
                    contato.setNumeroDocumento(pegaTag(elementoBeneficiario1, "unimed:numeroGuiaOperadora"));

                    Element elementoBeneficiario2 = (Element) tagdadosGuia.item(y);
                    beneficiariosLote.add(pegaTag(elementoBeneficiario2, "unimed:valorProcessadoGuia"));
                    Element elementoBeneficiario3 = (Element) tagdadosGuia.item(y);
                    beneficiariosLote.add(pegaTag(elementoBeneficiario3, "unimed:valorLiberadoGuia"));

                    Element elementoBeneficiario03 = (Element) tagdadosGuia.item(y);
                    String valorGlosa = (pegaTag(elementoBeneficiario03, "unimed:valorGlosaGuia"));
                    contato.setTipoGlosa(valorGlosa);

                    /*Dados das Guias*/

                    /*Procedimentos*/
                    Element elementoBeneficiario4 = (Element) tagProcedimentos1.item(a);
                    beneficiariosLote.add(pegaTag(elementoBeneficiario4, "unimed:valorProcessado"));
                    Element elementoBeneficiario5 = (Element) tagProcedimentos1.item(a);
                    beneficiariosLote.add(pegaTag(elementoBeneficiario5, "unimed:dataProcedimento"));
                    //contato.setData(pegaTag(elementoBeneficiario5, "unimed:dataProcedimento"));

                    String d = pegaTag(elementoBeneficiario5, "unimed:dataProcedimento");
                    //Primeiro converte de String para Date
                    DateFormat formatUS = new SimpleDateFormat("yyyy-mm-dd");
                    Date date = null;
                    try {
                        date = formatUS.parse(d);
                    } catch (ParseException ex) {
                        Logger.getLogger(LeitorXml1.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    //Depois formata data
                    DateFormat formatBR = new SimpleDateFormat("dd-mm-yyyy");
                    String dateFormated = formatBR.format(date);

                    contato.setData(dateFormated);
                    for (int b = 0; b < tagProcedimento.getLength(); b++) {

                        //Procedimento(tag que fica dentro de procedimentos)!!
                        Element elementoBeneficiario6 = (Element) tagProcedimento.item(0);
                        beneficiariosLote.add(pegaTag(elementoBeneficiario6, "unimed:codigo"));
                        contato.setProcedimento(pegaTag(elementoBeneficiario6, "unimed:codigo"));

                        Element elementoBeneficiario7 = (Element) tagProcedimento.item(0);
                        beneficiariosLote.add(pegaTag(elementoBeneficiario7, "unimed:descricao"));
                        contato.setDescricaoProced(pegaTag(elementoBeneficiario7, "unimed:descricao"));

                        //Procedimento(tag que fica dentro de procedimentos)!!
                    listaLote.add(contato);                       
}

Updating: inserir a descrição da imagem aqui

In the image the description that is Syringe... It is repeated by the others because it is the first data of the xml file, I can only get all the data when I instate the object inside the FOR

If you want to check the . xml snippet, it is here:

<unimed:procedimentos>
                    <unimed:dadosProcedimento>
                      <unimed:seqLote>2</unimed:seqLote>
                      <unimed:seqGuia>2</unimed:seqGuia>
                      <unimed:seqProc>1</unimed:seqProc>
                      <unimed:procedimento>
                        <unimed:codigo>22222</unimed:codigo>
                        <unimed:tipoTabela>19</unimed:tipoTabela>
                        <unimed:descricao>SERINGA S/AG.20ML LUER SLIP PLASTIP</unimed:descricao>
                      </unimed:procedimento>
                      <unimed:quantidadeExecutada>3</unimed:quantidadeExecutada>
                      <unimed:valorProcessado>4.53</unimed:valorProcessado>
                      <unimed:valorLiberado>4.53</unimed:valorLiberado>
                      <unimed:dataProcedimento>2015-03-26</unimed:dataProcedimento>
                      <unimed:valorFilme>0</unimed:valorFilme>
                      <unimed:valorServico>4.5300</unimed:valorServico>
                      <unimed:valorHonorario>0</unimed:valorHonorario>
                      <unimed:numAutorizacao>4111421</unimed:numAutorizacao>
                      <unimed:prestadorExecutante>44444</unimed:prestadorExecutante>
                      <unimed:valorInformado>4.5300</unimed:valorInformado>
                    </unimed:dadosProcedimento>
                    <unimed:dadosProcedimento>
                      <unimed:seqLote>2</unimed:seqLote>
                      <unimed:seqGuia>2</unimed:seqGuia>
                      <unimed:seqProc>2</unimed:seqProc>
                      <unimed:procedimento>
                        <unimed:codigo>333333</unimed:codigo>
                        <unimed:tipoTabela>19</unimed:tipoTabela>
                        <unimed:descricao>SCALP NR.27G MEDSCALP EMB.PLAST.</unimed:descricao>
                      </unimed:procedimento>
                      <unimed:quantidadeExecutada>1</unimed:quantidadeExecutada>
                      <unimed:valorProcessado>2.58</unimed:valorProcessado>
                      <unimed:valorLiberado>2.58</unimed:valorLiberado>
                      <unimed:dataProcedimento>2015-03-26</unimed:dataProcedimento>
                      <unimed:valorFilme>0</unimed:valorFilme>
                      <unimed:valorServico>2.5800</unimed:valorServico>
                      <unimed:valorHonorario>0</unimed:valorHonorario>
                      <unimed:numAutorizacao>4444444</unimed:numAutorizacao>
                      <unimed:prestadorExecutante>4444444</unimed:prestadorExecutante>
                      <unimed:valorInformado>2.5800</unimed:valorInformado>
                    </unimed:dadosProcedimento>
                    <unimed:dadosProcedimento>
                      <unimed:seqLote>2</unimed:seqLote>
                      <unimed:seqGuia>2</unimed:seqGuia>
                      <unimed:seqProc>3</unimed:seqProc>
                      <unimed:procedimento>
                        <unimed:codigo>4444444</unimed:codigo>
                        <unimed:tipoTabela>19</unimed:tipoTabela>
                        <unimed:descricao>AGULHA HIPODERMICA 40X12 PRECISIONG</unimed:descricao>
                      </unimed:procedimento>
  • I don’t understand very well, you want a new object like the previous one at the beginning of the third one and then within it you change the procedimento and the descricaoProced?

  • This, the xml I have is misspelled and this was the solution I found to get several tags. Inside the third is, it is not repeating the data, IE, just takes the last data. I will show an image

  • 1

    Your listaLote.add(contato) is in or out of the last for? Basically if you put in it will work, it will add 1 object to each tag. Outside it only takes the last one anyway, because every iteration of the is overwritten the previous one, an alternative would be to create a list of tags and set it in the object.

  • He’s inside the last For, there’s one more who’s responsible for Taking the Main Node

  • I edited the question

  • And I was wrong too, it’s not the last element being caught but the first

  • I did, I put the list inside the last one for, I put it up there right after the second one and it worked. Put an answer so I can mark it as a solution

  • 1

    @Diegoaugusto yourself can publish your answer and mark it as the correct answer, there is nothing wrong with it.

Show 3 more comments

1 answer

0


I added the listaLote.add(contato) within the last FOR and my problem was solved

Browser other questions tagged

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