tags:

views:

18

answers:

1

Hi all,

I am trying to learn how to import from XML to JTable. I am analyzing the following code in an attempt to understand what is happening. The problem is I am unable to figure out why am I failing to see any values in the JTable. I am sure that the XML is being parsed using the DOMBuilder etc. I have included the code and the XML file that I am using.
Many thanks in advance! P.S. Place the person.xml file into the project folder.

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class XMLInJTable extends AbstractTableModel {
        Vector data;
        Vector columns;

        public XMLInJTable() {
                try {
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        Document doc = db.parse("person.xml");

                        NodeList nl = doc.getElementsByTagName("Name");
                        NodeList n2 = doc.getElementsByTagName("Address");
                        NodeList n3 = doc.getElementsByTagName("ContactNo");
                        NodeList listOfPersons = doc.getElementsByTagName("person");
                        String data1 = "", data2 = "", data3 = "";
                        data = new Vector();
                        columns = new Vector();
                        for (int i = 0; i < listOfPersons.getLength(); i++) {
                                data1 = nl.item(i).getFirstChild().getNodeValue();
                                data2 = n2.item(i).getFirstChild().getNodeValue();
                                data3 = n3.item(i).getFirstChild().getNodeValue();
                                String line = data1 + " " + data2 + " " + data3;
                                StringTokenizer st2 = new StringTokenizer(line, " ");
                                while (st2.hasMoreTokens())
                                        data.addElement(st2.nextToken());
                        }
                        columns.add("");
                        columns.add("");
                        columns.add("");

                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public int getRowCount() {
                return data.size() / getColumnCount();
        }

        public int getColumnCount() {
                return columns.size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
                return (String) data.elementAt((rowIndex * getColumnCount())
                                + columnIndex);
        }

        public static void main(String argv[]) throws Exception {
                XMLInJTable t = new XMLInJTable();
                JTable table = new JTable();
                table.setModel(t);
                JScrollPane scrollpane = new JScrollPane(table);
                JPanel panel = new JPanel();
                panel.add(scrollpane);
                JFrame frame = new JFrame();
                frame.add(panel, "Center");
                frame.pack();
                frame.setVisible(true);
        }
}

person.xml

<?xml version="1.0"  encoding="UTF-8"  standalone="no"?>

<Person>
<Name>Angelina</Name>
<Address>Dehi</Address>
<ContactNo>111111</ContactNo>

<Name>Martina</Name>
<Address>Mumbai</Address>
<ContactNo>222222</ContactNo>

</Person>

Edit* I managed to get it working, it was because the line:

NodeList listOfPersons = doc.getElementsByTagName("person");

contained a small letter p for "persons" whenever it should've been a capital P. Now I've managed to display a single line of values for Angelina however it fails to display for Martina. Need to figure that out now with some much appreciated help thanks =)

A: 

The problem might be with the person.xml; it should be something like:

<?xml version="1.0"  encoding="UTF-8"  standalone="no"?>

<Persons>

<Person>
<Name>Angelina</Name>
<Address>Dehi</Address>
<ContactNo>111111</ContactNo>
</Person>

<Person>
<Name>Martina</Name>
<Address>Mumbai</Address>
<ContactNo>222222</ContactNo>
</Person>

</Persons>

Otherwise, listOfPersons.getLength() is always 1.

EDIT2: On another note, I suggest you to traverse through the node like the following pseudo code:

for each <person> in <persons>
  for each child in <person>
    if child tag name is "Name"
      # do something 
    else if child tag name is "Address"
      # do something
    else if child tag name is "ContactNo"
      # do something

This way you would make sure you're getting the correct details for each person.

William
Thanks for the reply, I had a feeling it was something to do with that. However upon changing the xml code it came up with an error along the lines of "xml document not formatted properly".
Joey jie
That's right. You need a single root element. Please see revised answer.
William
You sir, are a genius =) many thanks!
Joey jie
I'll try and implement that as it's a much more effective method. I'd to try and replace the vectors with an ArrayList as upon reading, Vectors appear to be obsolete now?
Joey jie
AFAIK, Vector is not obsolete at all. Here's an article explaining when to use `ArrayList` and `Vector`: http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html. In short, if you are not doing anything concurrent, `ArrayList` may give slightly better performance.
William