0

Estoy intentado visualizar un xml, en un jtable, por el momento solo me muestra el ultimo elemento del xml y el resto lo omite, quisiera saber dónde está el error.

public void llenar()
    {
    SAXBuilder builder = new SAXBuilder();
    File xmlFile = new File( "C:/ACME/empleados_ACME.xml" );
    try
    {
        Document document = (Document) builder.build( xmlFile );

        Element rootNode = document.getRootElement();

        List list = rootNode.getChildren( "empleado" );


        for ( int i = 0; i < list.size(); i++ )
        {
            Element tabla = (Element) list.get(i);

            String nombreTabla = tabla.getAttributeValue("cédula");           

            List lista_datos = tabla.getChildren();

            Object[][] listaTable = new Object[lista_datos.size()][9];


            for ( int j = 0; j < lista_datos.size(); j++ )
            {
                Element datos = (Element)lista_datos.get( j );

                listaTable [j][0] = nombreTabla;

                String nombre  = datos.getChildTextTrim("nombre_completo");
                String[] parts = nombre.split(" ");

                listaTable[j][1] = parts[0];

                listaTable[j][2] = parts[1];

                listaTable[j][3] = parts[2];

                listaTable[j][4] = datos.getChildTextTrim("teléfono1");

                listaTable[j][5]= datos.getChildTextTrim("teléfono2");

                listaTable[j][6] = datos.getChildTextTrim("email");

                listaTable[j][7] = datos.getChildTextTrim("dirección_domicilio");

                listaTable[j][8] = datos.getChildTextTrim("fecha_nacimiento");

                System.out.println( "\t"+listaTable [j][0]+"\t"+listaTable [j][1]+"\t\t"+listaTable [j][2]+"\t\t"+listaTable [j][3]+"\t\t"+listaTable [j][4]+"\t\t"+listaTable [j][5]+"\t\t"+listaTable [j][6]+"\t\t"+listaTable [j][7]+"\t\t"+listaTable [j][8]);

            }
                    xmlTable.setModel(new javax.swing.table.DefaultTableModel(
            listaTable,
            new String [] {
                "Cedula", "Nombre", "Apellido", "Apellido", "Telefono 1", "Telefono 2", "Email", "Direccion", "Fecha",
            }
        ));
        }

    }catch ( IOException io ) {
        System.out.println( io.getMessage() );
    }catch ( JDOMException jdomex ) {
        System.out.println( jdomex.getMessage() );
    }

    }
2
  • Nunca he utilizado JTable pero parece que tienes que crear un modelo con todos los datos y pasarlo al método ºsetModelº, es decir, tienes que mover el código de crear el modelo fuera del bucle for ( int i = 0; i < list.size(); i++ ) y también ponerlo de xmlTable.setModel después del bucle. el 15 jun. 2017 a las 20:01
  • Gracias por contestar, al final lo que hice fue cargar un array con la información del xml, y mostrar el array en el jtable
    – Arga17
    el 22 jun. 2017 a las 17:30

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.