In this section, you will learn how to access the contents or data of xml document and display its information into the jtable.
In this section, you will learn how to access the contents or data of xml document and display its information into the 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"?> <Person> <Name>Angelina</Name> <Address>Dehi</Address> <ContactNo>111111</ContactNo> <Name>Martina</Name> <Address>Mumbai</Address> <ContactNo>222222</ContactNo> </Person> |
Here is the code:
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); } }