Java working with XML overview (tutorial)

Complete Source Code for all examples below

XML is widely used technology in java world, many people even make jokes that java is just convenience language to parse XML (joke), but have you wondered what is there actually to know to be able to work with XML at least on the basic level? So, I decides to refresh my knowledge and help beginers by providing this overview of what is what in the Java XML world.

I guess we will start with terminology first:

JAXP (Java API for XML Processing) - This is basically a specification to outline number of tools available in java to work with XML. Many of you will hear things like DOM and SAX when talking about JAXP.

SAX (Simple API for XML) [jump to examples] is the basic approach to the XML parsing by means of envoking events, like start node, end node, inner text and so on. The way it works in a nutshell is by feeding the XML to SAX parser that employs the event handler to notify us when things occur. Naturally it is up to us to decide what we are going to do with that. So the decision is ours on what to do with whatever comes from XML.

DOM (Document Object Model) [jump to examples] - Uses a more restrictive approach in the sense that it makes all the decisions what to do with XML data - and basically it loads it all. Behind the scenes DOM is, in fact, using SAX. So this approach allows us to load full XML documents in to memory. This may be to intense in the sense of memory usage but it does allow things like editing the document and searching it for orbitary data for example by using XPath.

XPath [jump to examples] - I tend to think of it like SQL for XML. I know this is a weird way of putting it but basically XPath provides a syntax for selecting nodes or values from XML.

JAXB (Java API for XML Binding) - is java library that allows to bind the XML nodes to java objects. New versions include annotation binding whereby java class is annotated and whilst being marshalled (act of converting XML to Java Object) / unmarshalled (act of converting Java Object to XML) the library uses these annotation to decide how to accomplish the conversion. [jump to examples]

NOTE: many people new to java XML processing odten ask what is better DOM vs SAX vs JAXB. I hope the explanation above had set the idea in your mind that all of them are created for different purposes and may not be compared. So, you need to use the right tool for the right occasion.

Other fruity bits include XML Validation [jump to examples] which is done against an XML schema to help you validate your XML documents, and XML Transformations [jump to examples] which are done using XSL (Extensible Stylesheet Language) in order to transform your XML data into information, say HTML page, or an e-mail.

 

I know there is far more to java XML but remember this is only a basic overview, enjoy!

This page was last updated on: 03/07/2012 18:59