27 December, 2012

You Should Know, Java XML Parsing

As Java developer you should know that:

JAXP, Java API for XML Processing, lets you use any conforming parser implementation in a standard way. The code should be much more portable, and when you realise that a specific parser has grown too old, you can replace it with another without changing a line of your code (if you do it correctly).

    Basically there are three ways of handling XML in a standard way:
  1. SAX This is the simplest API. You read/modify the XML by defining a Handler class that receives the data inside elements/attributtes when the XML gets processed in a serial way. It is faster and simpler if you only plan to read some attributes/elements and/or write some values back (your case).
  2. DOM This method creates an object tree which lets you modify/access it randomly so it is better for complex XML manipulation and handling.
  3. StAX This is in the middle of the path between SAX and DOM. You just write code to pull the data from the parser you are interested in when it is processed.

Forget about propietary APIs such as Jdom or Apache ones (i.e. Apache Xerces XMLSerializer) because will tie you to a specific implementation that can evolve in time or lose backwards compatibility, which will make you change your code in the future when you want to upgrade to a new version of Jdom or whatever parser you use. If you stick to Java standard API (using factories and interfaces) your code will be much more modular and maintenable.

    Code samples
  1. XML parsing using SaxParser with complete code

No comments:

Post a Comment