Wednesday, October 15, 2008

Working with XML

When work with XML, most of time we start with a question of which technology or library should be used to speed up the development and also server the biz spec.

One guy summarized it in an article and here is the excerpt:

Which tool should I pick?

If you're new to XML you might not quite understand the need for having several XML tools. You might think that an API that allows you to read and write an XML document would be sufficient. The need for several tools arises when you realize that XML can be used in a lot of very different situations. Some examples are:

  • configuration files--or parameter files--are often written in XML. They typically contain technical data for systems like web servers.
  • XML files can be used when sending updates of business data to an application. Think of a list of new customers, orders or exchange rates.
  • XML data sometimes have to be persisted on files or in databases, but more often XML is only a format held in memory.
  • XML can be used as a description language for other types of XML documents. An example is XML schemas that are used to validate other XML documents.

Let's look at some of the different situations where you need to read an XML document, for example from a file.

When reading a configuration file, you need to get some or all of the parameters extracted from the file and then stored somewhere in your program. Digester is a great tool for this kind of operation. An introduction to Digester is "Digesting XML documents". Digester is also a natural choice if you only want to read some of the data in an XML file.

If you want to read all the data in an XML file and then continue to do some processing with these data, then you need to convert the XML into some kind of Java object structure. The simplest way to do this is to convert into JavaBeans, since beans are simple to work with. My own favorite tools for converting between XML files and JavaBeans are Castor and XMLBeans. Two articles that give you more information are " Converting XML documents to Java objects with Castor XML" and "Converting XML to JavaBeans with XMLBeans". XMLBeans is currently my number one pick since it seems capable of handling even the most complex XML structures and automatically packs the generated JavaBeans into a jar-file.

Castor and XMLBeans can be used when your XML files have a known, fixed structure that doesn't change over time. Castor and XMLBeans typically take the XML schemas that define the XML structures and generate beans for you. This is something you'll have to do manually, so you need an XML format that doesn't change too often.

If the format of your XML data is not fixed, you need to look for another tool. JDOM is such a tool, since it'll read any XML file and produce a tree-like structure in memory. This structure is very easy to work with for a Java programmer because of its simple API and its use of well-known Java objects like those implementing the List interface.

"

For full article, read from here .

Additional reading list:

Working with JDOM, XPath and XSLT

Java and JDOM: the perfect couple

Parsing an XML Document with XPath

Tuesday, October 14, 2008

Java + Excel XLS

Currently, I am doing a research on various solutions available for java & excel xls integration to be used for our next generation sales quotation tools. I found this article already did some work and compile a list of solution available below:

Library / package License Description
Actuate Spreadsheet Engine Commercial, 30-day trial version available Write Excel (XLS) files.
ExcelAccessor ?
Read and write Excel (XLS) files.
ExcelReader ? JavaWorld article on how to read Microsoft Excel (XLS) files, including code. Requires an installed Microsoft ODBC driver for Excel files, and Sun's ODBC-JDBC driver.
ExtenXLS Commercial, 30-day trial version available Read and write Microsoft Excel (XLS) files.
JACOB project LGPL Java COM bridge with examples to interface Excel.
Java Excel API LGPL Read Excel (XLS) 97, 98 and 2000 files.
Java to Excel conversion ? Write SYLK files, which Excel can import and export.
JExcel Commercial Swing component to integrate Excel via JNI.
jXLS LGPL Create Excel files using XLS templates.
POI Apache Software License 1.1 Read and write Microsoft OLE 2 compound document format files. This includes MS Office files (DOC, XLS, PPT) written with Office versions that were released after 1997.
Snowbound Commercial Read Excel files.
SoftArtisans OfficeWriter Commercial Write Word and Excel files.
Vista JDBC Commercial, 15-day trial version available JDBC driver to access MS Excel (XLS) files.
xlSQL GPL JDBC driver to access MS Excel (XLS) and CSV files.

In addition to this table, following links are viable choice for java & excel integration in the application.

Handle Excel files

Content Tagged with excel + Java

ExtenXLS Java Spreadsheet SDK

XLSBeans - mapping Excel sheets to POJOs

Java Excel API

JACOB - JAVA-COM Bridge to call COM components

The Java-Excel solution

Read and Write Microsoft Excel Documents with Jakarta's POI - (The Excel Document Model)

Excel Reports with Apache Cocoon and POI

Jxcell

j-integra - Accessing Excel from Java

ActiveXLS™ Professional for Java

Apache POI - Java API To Access Microsoft Format Files

jXLS - generating Excel files using XLS templates

´