List returns only last element

Asked

Viewed 1,988 times

0

Eae guys, after a lot of work I managed to get several xml tags to play in an Excel report. But when I populate the list with my object and step into the report only the last element is listed. I’ve made some System.out.println to show the list elements but from the same, only the last element appears. Can anyone help? I made another list to test with the beneficiary nameLote and I haven’t taken it yet, with it I can list everything when I do a FOR, but I don’t know how to pass a list that has no link with an object to Excel. Thank you


SUMMARY:

I want to take the data from the list and fill in an object called Unimedlote; mine listaLote returns the last element all rethought, ie, I think the values of my list are being replaced with every iteration of FOR that takes the data of the tags in xml. When I debug a line with Sysout picking up the contact object it lists normal: inserir a descrição da imagem aqui

But when I try to display the list this is what appears: inserir a descrição da imagem aqui

This image shows only one excerpt because the list is extensive, but the same client is repeated throughout the list.

public List<UnimedLote> realizaLeituraXML(String arquivoXML) throws ParserConfigurationException, SAXException, IOException {
    //fazer o parse do arquivo e criar o documento XML
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(arquivoXML);

    Element elem = doc.getDocumentElement();
    NodeList tagdadosLote = elem.getElementsByTagName("unimed:dadosLote");
    List<UnimedLote> listaLote = new ArrayList<>();
    System.out.printf("\n tagdadosLote %s ", tagdadosLote.getLength());
    UnimedLote contato = new UnimedLote();
    for (int i = 0; i < tagdadosLote.getLength(); i++) {
        String lote = "";

        Element elementoLote = (Element) tagdadosLote.item(i);
        lote = pegaTag(elementoLote, "unimed:numeroLote");
        contato.setNumLote(lote);

        NodeList tagGuia = (NodeList) elementoLote.getElementsByTagName("unimed:guia");
        // Como sabemos pela estrutura que só tem 1 elemento não necessitamos de um for podendo fixar o indice.
        NodeList tagdadosGuia = ((Element) tagGuia.item(0)).getElementsByTagName("unimed:dadosGuia");
        System.err.println("LOTE: " + lote);
        for (int y = 0; y < tagdadosGuia.getLength(); y++) {
            NodeList tagProcedimentos = ((Element) tagdadosGuia.item(y)).getElementsByTagName("unimed:procedimentos");
            NodeList tagBeneficiario0 = ((Element) tagdadosGuia.item(y)).getElementsByTagName("unimed:beneficiario");
            NodeList tagProcedimentos1 = ((Element) tagProcedimentos.item(0)).getElementsByTagName("unimed:dadosProcedimento");
            NodeList tagProcedimento = ((Element) tagProcedimentos1.item(0)).getElementsByTagName("unimed:procedimento");
            /*Beneficiarios*/
            Element elementoBeneficiario0 = (Element) tagBeneficiario0.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario0, "unimed:numeroCarteira"));
            contato.setCodigo(pegaTag(elementoBeneficiario0, "unimed:numeroCarteira"));

            Element elementoBeneficiario = (Element) tagBeneficiario0.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario, "unimed:nomeBeneficiario"));
            contato.setNomeBeneficiario(pegaTag(elementoBeneficiario, "unimed:nomeBeneficiario"));
            /*Beneficiarios*/

            /*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"));
            /*Dados das Guias*/

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

            //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)!!

            Element elementoBeneficiario8 = (Element) tagProcedimentos1.item(0);
            beneficiariosLote.add(pegaTag(elementoBeneficiario8, "unimed:quantidadeExecutada"));
            contato.setQtdExecutada(pegaTag(elementoBeneficiario8, "unimed:quantidadeExecutada"));

            Element elementoBeneficiario9 = (Element) tagProcedimentos1.item(0);

            String filme = pegaTag(elementoBeneficiario9, "unimed:valorFilme");
            if (filme
                    == null) {
                filme = "0";
            }
            BigDecimal vp = new BigDecimal(filme).setScale(2);
            contato.setCustoFilme(vp);
            beneficiariosLote.add(vp);

            Element elementoBeneficiario10 = (Element) tagProcedimentos1.item(0);
            String custo = pegaTag(elementoBeneficiario10, "unimed:valorServico");
            if (custo
                    == null) {
                custo = "0";
            }
            BigDecimal vc = new BigDecimal(custo).setScale(2);
            contato.setCustoServico(vc);
            beneficiariosLote.add(vc);

            Element elementoBeneficiario11 = (Element) tagProcedimentos1.item(0);

            String hon = pegaTag(elementoBeneficiario11, "unimed:valorHonorario");
            if (hon
                    == null) {
                hon = "0";
            }
            BigDecimal vh = new BigDecimal(hon).setScale(2);
            contato.setHonorario(vh);
            beneficiariosLote.add(vh);
            //Dados Guias
            Element elementoBeneficiario12 = (Element) tagdadosGuia.item(y);

            String tot = pegaTag(elementoBeneficiario12, "unimed:valorInformado");
            if (tot
                    == null) {
                tot = "0";
            }
            BigDecimal vt = new BigDecimal(tot).setScale(2);
            contato.setValorTotalInformado(vt);
            beneficiariosLote.add(vt);
            /*Dados das Guias*/
            System.err.println("Contatos: " + contato);    
            listaLote.add(y,contato);

        }

    }

    System.err.println("Lista lote: "+listaLote);
    return listaLote;

}

Excerpt where I fill the columns in Excel: //Go through the list and fill in the cells

        int cont2=0;
        for (int i = 0; i < listaContatoslt.size(); i++) {


            XSSFRow cabRowAnalitico7 = sheet1.createRow(i + 6);
            for (int x = 0; x <= 12; x++) {
                cabRowAnalitico7.createCell(x);
            }

            XSSFCell cellAnaA5 = cabRowAnalitico7.getCell(0);
            cellAnaA5.setCellStyle(estiloS2);
            cellAnaA5.setCellValue(listaContatoslt.get(i).getNumeroDocumento());

            XSSFCell cellAnaB5 = cabRowAnalitico7.getCell(1);
            cellAnaB5.setCellStyle(estiloS2);
            cellAnaB5.setCellValue(listaContatoslt.get(i).getCodigo());

            XSSFCell cellAnaC5 = cabRowAnalitico7.getCell(2);
            cellAnaC5.setCellStyle(estiloS2);
            cellAnaC5.setCellValue(listaContatoslt.get(i).getNomeBeneficiario());
            cont2++;
            }
  • 1

    Which object? , which part, specifies limiting to the maximum, or it would take a lot of time to try to help you

  • I edited the question, I don’t know if it got better but it’s hard even to explain

1 answer

3


The error is in you creating a single object of the type UnimedLote before the for

This line

UnimedLote contato = new UnimedLote();

Must stay inside your for.

  • Poxa traveled cool, thank you so much for helping me.

Browser other questions tagged

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