Share on Google+Share on Google+

Add XML to JTable

In this section, you will learn how to access the contents or data of xml document and display its information into the jtable.

Add XML to JTable

In this section, you will learn how to access the contents or data of xml document and display its information into the jtable. Java provides two xml parsers to read xml data. Here we are using DOM parser.

Description  Of  Code:

Parse the xml file using the classes DocumentBuilder and Document. Then retrieve all the elements in document order with a given tag name from the document and stored in the NodeList. Now in order to get values from node or you can say xml tags, use getFirstChild().getNodeValue() methods of NodeList class. These values are then stored in vector through the StringTokenizer. As we have extended our class with DefaultAbstractTable class, so we can inherit the methods of this class. Therefore using the setModel() method, we have stored all the xml file data into the JTable .

Here is the person.xml file:

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




Here is the code:

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

		} catch (Exception e) {

	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();
		JScrollPane scrollpane = new JScrollPane(table);
		JPanel panel = new JPanel();
		JFrame frame = new JFrame();
		frame.add(panel, "Center");


Posted on: April 26, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null