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

No comments:

´