Is there an alternative to multiple while loops?

Asked

Viewed 105 times

7

I have a method alterar() that change the data of a particular element of my XML Person, however, this routine uses several whiles one for each element, see below the routine:

public void alterar() {                 
    try {
        Document doc = parse(arquivo);                        
        
        List<Node> nos = doc.selectNodes("//pessoas/pessoa[idpessoa='" + String.valueOf(idPessoa) + "']");
        
        for (Node no: nos) {
            Element elemPessoa = (Element) no;
            
            Iterator<Element> itrIdPessoa = elemPessoa.elementIterator("idpessoa");
            
            while (itrIdPessoa.hasNext()) { 
                Element elemIdPessoa = (Element) itrIdPessoa.next();
                elemIdPessoa.setText(String.valueOf(idPessoa));
            }
            
            Iterator<Element> itrNome = elemPessoa.elementIterator("nome");
            
            while (itrNome.hasNext()) { 
                Element elemNome = (Element) itrNome.next();
                elemNome.setText(nome);
            }
            
            Iterator<Element> itrEmail = elemPessoa.elementIterator("email");
            
            while (itrEmail.hasNext()) { 
                Element elemEmail = (Element) itrEmail.next();
                elemEmail.setText(email);
            }
            
            Iterator<Element> itrCelular = elemPessoa.elementIterator("celular");
                            
            while (itrCelular.hasNext()) { 
                Element elemCelular = (Element) itrCelular.next();
                elemCelular.setText(celular);
            }
        }
                    
        escrever(doc, arquivo);
    }                 
    catch (DocumentException ex) {
        throw new RuntimeException();
    } 
    catch (IOException ex) {
        throw new RuntimeException();
    }        
    catch (NoClassDefFoundError ex) { 
        System.err.println("Erro: " + ex.getMessage());
    }
}    

This is the structure of the Pessoa XML file:

<?xml version="1.0" encoding="UTF-8"?>
<pessoas>
   <pessoa>
      <idpessoa>2</idpessoa>
      <nome>Marcielli teste</nome>
      <email>[email protected]</email>
      <celular>88888888</celular>
   </pessoa>
   <pessoa>
      <idpessoa>3</idpessoa>
      <nome>Ana</nome>
      <email>[email protected]</email>
      <celular>1233121</celular>
   </pessoa>
</pessoas>

Doubt

I would like to know if possible, if there is any alternative to all of these whiles?

PS: I am using the dom4j library to process and read the XML file.

  • For example: Iterator<Element> itrIdPessoa = elemPessoa.elementIterator("idpessoa"); it returns here how many items? because each no is an item of this XML?

  • @Virgilionovic ai returns a theoretically, the no in Sso can consider the tag pessoa.

  • For me @Denercarvalho are unnecessary interactions I believe everything is there...

1 answer

9


All that exists a certain pattern is possible to abstract and make the code generic. Then it is possible to create a method that makes this code in a generic way by "filling in the gaps" that are specialized. Example (not necessarily the best):

private void FillElements(Node no, String nomeNo, String valor) {
    Element elemPessoa = (Element)no;
    Iterator<Element> iterador = elemPessoa.elementIterator(nomeNo);
    while (iterador.hasNext()) { 
        Element elemento = (Element)iterador.next();
        elemento.setText(valor);
    }
}

Using:

FillElements(no, "idpessoa", String.valueOf(idPessoa)) //preenche as lacunas
FillElements(no, "nome", nome)
FillElements(no, "email", email)
FillElements(no, "celular", celular)

I put in the Github for future reference.

I am not entering into the merit if the code is correct and does what is desired or if it is well written and could be better, even for not knowing the full context, I’m just showing how to generalize the code posted and avoid repetition.

This can be considered DRY.

Browser other questions tagged

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