Transform DOM to TableModel

L

Larry Coon

I'm writing an application which will invoke a servlet (via a
UrlConnection) and display the (CML) data returned by the servlet
in a JTable. (The data returned by this servlet is in row/column
format, so in this application a JTable is the appropriate control
for displaying the data.) Can someone give me advice on the best
approach for converting a DOM to a TableModel:

-- Write a Transformer that takes a Document as a source and
uses a TableModel as a sink?

-- Use XSLT?

-- Use SAX instead of DOM (I'm not married to DOM) and build
the TableModel as thx SAX parser parses it?

Any advice would be appreciated.


Larry Coon
University of California
 
P

pit.grinja

Hi Larry,
I'm writing an application which will invoke a servlet (via a
UrlConnection) and display the (CML) data returned by the servlet
in a JTable. (The data returned by this servlet is in row/column
format, so in this application a JTable is the appropriate control
for displaying the data.) Can someone give me advice on the best
approach for converting a DOM to a TableModel.
Are we dealing with a DOM tree or a table? I believe that your XML data
has a structure which was designed to represent tabular data.
-- Write a Transformer that takes a Document as a source and
uses a TableModel as a sink?
I would insert one more step: extract the data that you want from your
DOM and convert that into a 2 dimensional data structure (Arraylist of
arraylists or a 2D object[][])and then warp a TableModel around that
data structure. That will make the implementation of the TableModel
easier than directly accessing the DOM. Furthermore, the memory-hungry
DOM will probably not gc`ed when it is referenced from within the
TableModel.
-- Use XSLT?
No. You still would have to wrap your TableModel around the data that
result from the transformation.
-- Use SAX instead of DOM (I'm not married to DOM) and build
the TableModel as thx SAX parser parses it?
Since your data seem to have a rather flat, 2D like structure, SAX
could be the better choice here, indeed. Personally, I have never used
SAX before (DOM always seemd to be the solution with the lesser
headaches), but SAX is "leaner". So when you have to deal with huge
files with a simple structure that won´t change from file to file, SAX
is the way to go. As outlined above, its probably easier to first build
a separate 2D data structure from your SAX events, and when this is
done, wrap your TableModel around your data. Or, if you really want to
experiment, connect the SAX parser somehow to your tablemodel, and
whenever the SAX Parser fires an event, make the TableModel fire a
suitable event to notify the JTable about the event, and watch how the
JTable is build up "life" (forget about this last suggestion in a
production environment).
HTH
Piet
 
L

Larry Coon

I would insert one more step: extract the data that you want from your
DOM and convert that into a 2 dimensional data structure (Arraylist of
arraylists or a 2D object[][])and then warp a TableModel around that
data structure. That will make the implementation of the TableModel
easier than directly accessing the DOM. Furthermore, the memory-hungry
DOM will probably not gc`ed when it is referenced from within the
TableModel.

Hi Piet,

Thanks for the advice. This is what I ended up doing, and it worked
great.


Larry Coon
University of California

The NBA Salary Cap FAQ:
http://members.cox.net/lmcoon/salarycap.htm
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top