users@jaxb.java.net

Re: JTable to XML and back

From: CLIFF.WONG <CLIFF.WONG_at_oracle.com>
Date: Mon, 25 Nov 2002 07:15:11 -0800

Ed,

JAXB may not be the best way to manipulate JTables, since JTables contain abstract datatypes. If you fix them to a single type, will make a jaxb content tree very rigid (i.e. not robust). Basically, you will have a XSD containing just [Rows] and [Columns] and the single datatype. If you are going down that route (rows, columns, single datatype), I would just create an array of vectors (via TableModel) and write them out through a SAX parser and vice-versa on reading--it will be much more efficient compared to JAXB or DOM and just as robust.

If you have different datatypes in the JTable, then it gets exponentially complex, such that your XSD can get very complex, which is why JAXB was created. If you JTable has a specific data-dependent rules, then just create an XSD and use it against you TableModel (then you can marshal/unmarshal the data).

As an easier alternative: since JTable contains a TableModel, you may want to write a routine to convert the TableModel to TreeModel then to XML. There are numerous examples of converting JTrees to DOM and back:

http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPDOM6.html

Good Luck,
Cliff W.