JAVA - I can not print completely from my XML

1

I am developing a program that allows me to print the elements of tags of an XML that I have, this is the instruction that this is my main class, that allows me to print a certain tag:

    for(int k=0; k < list3.getLength(); k++) {
                 Node node3 = list3.item(k);

                 if(node3.getNodeType() == Node.ELEMENT_NODE) {

                  Element element3 = (Element) node3;

                  Concepto concepto3 = new Concepto();

                      concepto3.setCodigo(element3.getElementsByTagName("Codigo").item(0).getTextContent());
                      concepto3.setDescripcion(element3.getElementsByTagName("Descripcion").item(0).getTextContent());
                      concepto3.setUT(element3.getElementsByTagName("UT").item(0).getTextContent());
                      concepto3.setMonto(element3.getElementsByTagName("Monto").item(0).getTextContent()); 

                      ListaConceptos.add(concepto3);
                      System.out.println(concepto3);

System.out.println(concepto.toString2());

            }
        }

And this is a piece of the labels that I need to print:

<OperacionesManoObra>
<OperacionManoObra>
<UT>0</UT>
<Monto>0</Monto>
</OperacionManoObra>
<OperacionManoObra>
<UT>0</UT>
<Monto>0</Monto>
</OperacionManoObra>
<OperacionManoObra>
<Codigo>SN</Codigo>
<Descripcion>VEHIC.EN BANCADA:COLOCAR-QUITAR</Descripcion>
<UT>15</UT>
<Monto>337.5</Monto>
</OperacionManoObra>
<OperacionManoObra>
<Codigo>SN</Codigo>
<Descripcion>BANCADA:PREPARAR</Descripcion>
<UT>10</UT>
<Monto>225</Monto>
</OperacionesManoObra>

Where I see the problem lies, is that the first two elements, only have the labels "UT" and "Amount", and the others are normal ... how could I fix it so that in the first two I printed null those who do not have ?, thanks.

    
asked by Antonio Alejos 27.11.2018 в 17:39
source

1 answer

0

The problem is that you do not check if there is a value or not, with what you try to access something that does not exist. You should check before:

for(int k=0; k < list3.getLength(); k++) {
    Node node3 = list3.item(k);

    if(node3.getNodeType() == Node.ELEMENT_NODE) {

        Element element3 = (Element) node3;

        Concepto concepto3 = new Concepto();

        //Devuelve null si no existe
        Element elem = element3.getElementsByTagName("Codigo").item(0);
        if (elem != null) { 
            concepto3.setCodigo(elem.getTextContent());
        }
        elem = element3.getElementsByTagName("Descripcion").item(0);
        if (elem != null) { 
            concepto3.setDescripcion(elem.getTextContent());
        }
        elem = element3.getElementsByTagName("UT").item(0);
        if (elem != null) { 
            concepto3.setUT(elem.getTextContent());
        }                      
        elem = element3.getElementsByTagName("Monto").item(0);
        if (elem != null) { 
            concepto3.setMonto(elem.getTextContent());
        }               

        ListaConceptos.add(concepto3);
        System.out.println(concepto3);

   }
}
    
answered by 27.11.2018 / 18:18
source