Help with logic

Asked

Viewed 127 times

4

I have a method that reads several tags from a file . xml and returns a list filled by the data read.

Problem: Each xml file has several batches and within the batch has several tabs, for example, I have a Client named Rodrigo and he is in a tab with an X procedure, the system can read this client, when passing to the other tab I have the Rodrigo Client again, but now in this guide he has several procedures: y, z, w... When my list is returned and I Gero the report he always repeats the name of the Client, thus:

Nome: Rodrigo
Procedimento X

Nome Rodrigo
Procedimento Y

Nome Rodrigo
Procedimento Z

Nome Rodrigo
Procedimento W

I wanted to show them off in such a way:

Nome Rodrigo
Procedimento X //Esse é p procedimento da primeira guia, como só tem um ele é separado

Nome Rodrigo //Na segunda guia o cliente Rodrigo possui 3 procedimentos, então todos são listados juntos
Procedimento Y
Procedimento Z
Procedimento W

Method:

public List<Procedimentos> 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<Procedimentos> listaLote = new ArrayList<>();
        System.out.printf("\n tagdadosLote %s ", tagdadosLote.getLength());
        for (int i = 0; i < tagdadosLote.getLength(); i++) {
            NumeroLote n = new NumeroLote();
            String lote = "";

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

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

            for (int y = 0; y < tagdadosGuia.getLength(); y++) {
                //Procedimentos proc = new Procedimentos();
                Procedimentos contato = new Procedimentos();
                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");

                Element elementoBeneficiarioname = (Element) tagdadosGuia.item(y);
                String nomeBeneficiario = (pegaTag(elementoBeneficiarioname, "unimed:nomeBeneficiario"));
                contato.setNomeBeneficiario(nomeBeneficiario);

                for (int a = 0; a < tagProcedimentos1.getLength(); a++) {
                    NodeList tagProcedimento = ((Element) tagProcedimentos1.item(a)).getElementsByTagName("unimed:procedimento");

                    for (int b = 0; b < tagProcedimento.getLength(); b++) {
                        //Aqui é onde pego os demais dados Do arquivo XML e passo para meu objeto "contato"
                        //Deixei vazio pois é um metodo muito extenso

                        listaLote.add(contato);
                    }

                }

            }

        }

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

    }

How can I list this data the way I want it? When I put it listaLote.add(contato); in the For who travels tagDadosGuia It lists the right Customer names, but only takes the first procedure thus:

Nome Rodrigo
Procedimento X

Nome Rodrigo
Procedimento Y //Para nesse procedimento e não pega os restantes Z e W

It only takes all the procedures if the listaLote.add(contato); stay inside the last For but when this happens he always repeats the names as shown above, Thank you.


Updated 25/05

My class Procedures:

public class Procedimentos {

    private String numLote;
    private String dataRealizacao;
    private String descriçãoServico;
    private String codTab;
    private String codigoServico;
    private String quantidadeExecutada;
    private BigDecimal valorProcessado;
    private BigDecimal valorLiberado;
    private BigDecimal valorGlosa;
    private String codigoGlosa;
    private String sequenciaGuiaProcedimento;
    private String sequenciaGuiaDados;
    private String nomeBeneficiario;
    private String valorLiberadoGuia;
    private String numeroGuiaSenha;

    private List<DetalhesProcedimentos> listaProcedimentos;
    ...

Dai in the method that reads the xml I declared:

 List<DetalhesProcedimentos> list = new ArrayList<>();

and within the for that runs through the tagProcedimento i did it:

DetalhesProcedimentos p1 = new DetalhesProcedimentos();
Element elementoBeneficiarioDescricaoServico = (Element) tagProcedimento.item(b);
                        beneficiariosLote.add(pegaTag(elementoBeneficiarioDescricaoServico, "unimed:descricao"));
                        p1.setDescricaoServico(pegaTag(elementoBeneficiarioDescricaoServico, "unimed:descricao"));

list.add(p1);
p.setListaProcedimentos(list);

And this is the Detail Processing Class:

public class DetalhesProcedimentos {

    private String dataRealizacao;
    private String descricaoServico;

    public String getDataRealizacao() {
        return dataRealizacao;
    }

    public void setDataRealizacao(String dataRealizacao) {
        this.dataRealizacao = dataRealizacao;
    }

    public String getDescricaoServico() {
        return descricaoServico;
    }

    public void setDescricaoServico(String descricaoServico) {
        this.descricaoServico = descricaoServico;
    }

    @Override
    public String toString() {
        return "DetalhesProcedimentos{" + "dataRealizacao=" + dataRealizacao + ", descricaoServico=" + descricaoServico + '}';
    }

}
  • What is the structure of this XML file?

  • So the archive is huge, it has 32k line. I used this lot of for because it was the only way I could get the data of the tags that are repeated, as the tagDadosGuia that is repeated and inside it the tag Procedures can be repeated as well. I’ll try to make an example.

  • Here is an example of the file, I edited it and decreases its size, note that the tagLote hasn’t even closed yet because only this lot is already large..

  • https://mega.co.nz/#! 7hwgEJRS! cf3CjpsXPmlLjvwA9L1DxdBG-eD86RfOeahcXohhDtw

  • We just need to see the structure.... not the file itself... At least for me with the structure it is easier to have a view of how to help.

  • This file that I upei is the structure, is that it is still a little big to post. It is not the whole file, only a part

  • I think the problem might be in this part Procedimentos contato = new Procedimentos(); he stays inside the last FOR that way he always repeats the names, and when I put him in the First FOR the names are correct, but the procedures that are completed in the last FOR are all repeated

Show 2 more comments

1 answer

4


From what I understand your class Procedures() will contain a contato (let’s assume it’s just the name) and a list of n procedimentos

So the pseudo code should be about that (I omitted parts of the parse of XML:

for (int y = 0; y < tagdadosGuia.getLength(); y++) {
    Procedimentos contato = new Procedimentos();
    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");

    Element elementoBeneficiarioname = (Element) tagdadosGuia.item(y);
    String nomeBeneficiario = (pegaTag(elementoBeneficiarioname, "unimed:nomeBeneficiario"));
    contato.setNomeBeneficiario(nomeBeneficiario);

    for (int a = 0; a < tagProcedimentos1.getLength(); a++) {
        NodeList tagProcedimento = ((Element) tagProcedimentos1.item(a)).getElementsByTagName("unimed:procedimento");

        for (int b = 0; b < tagProcedimento.getLength(); b++) {
            // Aqui você terá n procedimentos então       
            contato.addProcedimeto(magicaPraPegarProcedimento())     
            // O método magicaPraPegarProcedimento assume que vocêpega um procedimento do XML. E então armazena num array na classe Procedimentos()                         
        }

        listaLote.add(contato); // Add o contato a lista 
    }

}

Edition based on your update and in our discussion in the comments.

You got my suggestion a little wrong. In place of private List<Procedimentos> listaProcedimentos;

You will have a Procedure list, which will be a new Class, it can be a simple class that has only one string. Even you can only have one string list.

Thus remaining:

private List<String> listaProcedimentos;

What really matters is that you have a list on Procedimentos, where each element in that list describes a single procedure. It’s become clearer now?

So you can add in for more internal in this list and at the end Procedures() will contain a list, and then you will have the desired result.

Browser other questions tagged

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