Saturday, December 31, 2011

XML Parsing in Java


Parsing/Reading XML files can be a daunting task in itself. Whereas writing XML can be done manually, parsing manually is not an option. But, you don't worry, several java APIs have already been developed and released in the market to ease you in exploring an XML document. Broadly these APIs fall under two categories.

Tree-based APIs : (e.g. : JDOM )

Tree-based API maps an XML document into an internal tree structure, and then allows an application to navigate that tree. It maintains load up the entire XML data into memory, this means that we can access any part of the document any time - i.e random access. So, it is quite useful for manipulations of the XML tree, such as reordering elements, adding or deleting elements and attributes, renaming elements, and so on. The Document Object Model (DOM) working group at the World-Wide Web Consortium (W3C) maintains a recommended tree-based API for XML and HTML documents, and there are many such APIs from other sources.

Event-Based APIs : (e.g. : SAX )

An event-based API, on the other hand, uses calls to report parsing events to the application. The application deals with these events through customized event handlers. Such events include the start and end of elements and characters. Unlike tree-based APIs, event-based APIs usually do not build in-memory tree representations of the XML documents. Therefore, in general, an event-based API is useful for applications that do not need to manipulate the XML tree.

No comments :

Post a Comment