Joey jie
Joey jie

Reputation: 127

Help on XML to JTable Please

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 =)

Upvotes: 1

Views: 4401

Answers (1)

William Niu
William Niu

Reputation: 15853

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.

Upvotes: 1

Related Questions