IMIDEV 0 Newbie Poster

Hi every body,
I am trying to parse an xml file with sax parser in java and then fulfill a Jtable with the XML elements.
The parsing is well done, the Jtable is created but the problem is that all the elements are placed in the same row and the same row is repeated !! any Help please ! Here is my code :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package read;

//import java.util.ArrayList;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Read {

      public static Vector<Vector> rowData = new Vector<Vector>();
      public static Vector<String> rowOne = new Vector<String>();

   public static void main(String argv[]) {


    try {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    DefaultHandler handler;
           handler = new DefaultHandler() {
// private List<Personne> satff;
 boolean bstaff = false;
boolean bfname = false;
boolean blname = false;
boolean bnname = false;
boolean bsalary = false;
private int i;


            //Vector<String> rowOne = new Vector<String>();

public void startElement(String uri, String localName,String qName, 
    Attributes attributes) throws SAXException {

    System.out.println("Start Element :" + qName);
    if (qName.equalsIgnoreCase("staff")) {
        bstaff = true;
    //   int id = Integer.parseInt(attributes.getValue("id"));
     //  System.out.println("id_staff : " + id);
    }

    if (qName.equalsIgnoreCase("FIRSTNAME")) {
            bfname = true;
    }

    if (qName.equalsIgnoreCase("LASTNAME")) {
            blname = true;
    }

    if (qName.equalsIgnoreCase("NICKNAME")) {
            bnname = true;
    }

    if (qName.equalsIgnoreCase("SALARY")) {
            bsalary = true;
    }

}

public void endElement(String uri, String localName,
    String qName) throws SAXException {

    System.out.println("End Element :" + qName);
   if ("staff".equals(qName)){
     rowData.addElement(rowOne); 
     //rowOne.clear() ;
     System.out.println("pffffffffff");
    };
    /////

}


@Override
public void characters(char ch[], int start, int length)  {




     if (bfname) {         
         String s = new String(ch, start, length);
        rowOne.addElement(s);
            System.out.println("First Name : " + new String(ch, start, length));    
           //rowData.addElement(rowOne);

            bfname = false;

    }

    if (blname) {
        rowOne.addElement (new String(ch, start, length));
            System.out.println("Last Name : " + new String(ch, start, length));
            //rowData.addElement(rowOne);
            blname = false;

    }

    if (bnname) {
            rowOne.addElement (new String(ch, start, length));
            System.out.println("Nick Name : " + new String(ch, start, length));
             //rowData.addElement(rowOne);
            bnname = false;

    }

    if (bsalary) {
        rowOne.addElement (new String(ch, start, length));
            System.out.println("Salary : " + new String(ch, start, length));
             //rowData.addElement(rowOne);
            bsalary = false;

    }

System.out.println("longueur" + rowOne.size());
}





};

    saxParser.parse("file.xml", handler);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Vector<String> columnNames = new Vector<String>();
    columnNames.addElement("firstname");
    columnNames.addElement("lastname");
    columnNames.addElement("nickname");
    columnNames.addElement("salary");
    columnNames.addElement(" ");
    columnNames.addElement(" ");
    columnNames.addElement(" ");
    columnNames.addElement(" ");

    JTable table = new JTable(rowData, columnNames);

    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
     } catch (Exception e) {
       e.printStackTrace();
     }

   }

}

And Here is my XML document :

<?xml version="1.0" encoding="UTF-8"?>
<company>
    <staff >
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff >
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>

the table that I get is the following:

bb9ec3ddfcd44d44aa0ba029dff1b91c

I didn't inderstand what is the error !!! Thank you in advance :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.