Java Reference
In-Depth Information
Java Object Models
The final option in handling textual data is to create a set of Java classes that correspond to
the data being parsed. There are JSR proposals for doing this in JSON, but no standard. For
XML, this is accomplished using JAXB.
JAXB uses an underlying StAX parser, so configuring the best StAX parser for your plat-
form will help JAXB performance. The Java objects created via JAXB come from creating
an Unmarshaller object:
JAXBContext jc = JAXBContext . newInstance ( "net.sdo.jaxb" );
Unmarshaller u = jc . createUnmarshaller ();
The JAXBContext is expensive to create. Fortunately, it is thread-safe: a single global context
can be created and reused (and shared among threads). Unmarshaller objects are not thread-
safe; a new one must be created for each thread. However, the Unmarshaller object can be
reused, so keeping one in a thread-local variable (or keeping a pool of them) will help per-
formance when processing lots of documents.
Creating objects via JAXB is more expensive than either parsing or creating a DOM docu-
ment. The trade-off is that using those objects is much faster than walking through a docu-
ment (not to mention that using the objects is a simple matter of writing regular Java code,
instead of the convoluted API used to access documents). In addition, writing out the XML
that a set of JAXB documents represents is much faster than writing out the XML from a
document. Table 10-10 shows the performance differences for the sample 20-item document.
Table 10-10. JAXB marshalling and unmarshalling performance
Marshall Unmarshall
DOM 348 ms
298 ms
JAXB 414 ms
232 ms
Search WWH ::

Custom Search