tags:

views:

411

answers:

3

I am a c++ programmer but new to the java world. I have to display the xml data to a jtable directly.

Say,

the xml is of the following format

<Name> Tom </Name>
<DateofBirth> 12/3/1985 </DateofBirth>
<country> US </country>

Then the table needs to be dispalyed as follows

Name    |     DateofBirth | Country

Tom      12/3/1985       Us

Is it do-able? If so can any one please provide a sample?

A: 

It's certainly do-able.

I would check out the JDOM library, which provides a simple API to XML. It's easier to use than the standard but somewhat unwieldy DOM/SAX libraries.

If you have a schema for the above XML, JAXB may be of interest, although it's quite heavyweight.

I can't reliably advise on the Swing/JTable side, however. I'm sure there are lots of people who can look after that side of things.

Brian Agnew
+1  A: 

Yes, this is very do-able. There are 2 steps to this process. The first is to parse the xml. Some sample Java code for parsing xml would be (this example shows getting a "person" node from the xml file, but it can easily be adapted to your xml file):

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse("xml/sample.xml");
NodeList personNodes = document.getElementsByTagName("person");
List<String> names = new LinkedList<String>();
for (int i = 0; i < personNodes.getLength(); i++) {
  String firstName = null;
  String lastName = null;
  Node personNode = personNodes.item(i);
  NodeList children = personNode.getChildNodes();
  for (int j = 0; j < children.getLength(); j++) {
  Node child = children.item(j);
  String nodeName = child.getNodeName();
  String nodeValue = child.getTextContent();
  if ("firstName".equals(nodeName)) {
    firstName = nodeValue;
  } else if ("lastName".equals(nodeName)) {
   lastName = nodeValue;
  } 
}
names.add(firstName + " " + lastName);
}

Once you have extracted the data you need, created a new JTable that uses this data. The simplest JTable constructor to use for your purposes would be:

JTable(Object[][] rowData, Object[] columnNames)

There are more advanced and better ways to do this (such as with data binding frameworks), but this is definitely a good starting point.

Jeff Storey
Thanks for sharing the code, was able to find the same on the following page. Thanks!!!http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/
coolcake
No problem. I actually took it from my blog http://jeffastorey.blogspot.com/2009/10/getting-groovy-with-xml-parsing.html and showed how you can do it in only 2 lines with groovy (putting it in the JTable will be a couple more lines).
Jeff Storey
A: 

TableModel is an interface. If you really want to populate your table directly from the XML document, write a custom implementation of the TableModel interface which gets/sets values from your in-memory DOM object.

One advantage of this is that you can support editing the table, and apply the edits directly to your XML elements.

Just be aware that these TableModel methods are called VERY FREQUENTLY and thus should be very fast. If you're dealing with large XML documents, the random-access speeds for getting to an individual cell might not be speedy enough.

Sam Barnum