Overview
JAXB allows you to access XML documents like regular Java classes. It only works for documents that follow a fixed structure that can be described by a schema, a DTD or by a tree of Java classes.
The easiest way to use JAXB is with a XML Schema (XSD file). You can then just compile the schema using xjc. xjc generates annotated Java classes that you use to read and write the XML. Alternatively, you write your data structures as Java classes yourself and use annotations to map them onto the XML structure. An XML schema is not needed for this approach, but you can generate one from your code using the schemagen tool.
Once you have the annotated classes, JAXB can read XML documents and create object trees that represents them (this is called unmarshalling) and write them back into an XML document (marshalling). The object tree itself can be created, read and modified like any other Java object.
When should JAXB be used?
Use JAXB when..
- you want to easily read or write XML documents with a specific schema
- the documents are mainly structured data, not markup text (e.g. no XHTML or DocBook)
- throughput and performance is not your primary concern
Alternatives
- DOM provides a flexible object model that allows you to read and write any XML document. However, if your document has a known schema, using JAXB will be easier and less error-prone than the generic DOM model.
- StAX is a low-level pull API for reading any XML. It is fast and flexible, but also complicated to use and parsing a specific schema is hard to get right.
- SaX is a very old push XML API. Is is quite fast, but very difficult to use correctly. Avoid if possible.
- There are several XML APIs that are not part of any Java Edition. None of them is in wide-spread use anymore, even though they may have advantages over Java's APIs.
What do you need for JAXB?
For JAXB 2.x...
- Java SE 6 (or higher) ships with JAXB 2.x
- Java EE 5 (or higher) ships with JAXB 2.x
- JAXB for Java SE 5 can be downloaded here
- JAXB 2.x can not be used for versions < Java 5

