Skip to main content

Canonicalization support in JAXB RI 2.0

Posted by kohsuke on March 7, 2006 at 10:14 AM PST

JAXB 2.0 is sometimes used in an environment where its output needs to be canonicalized. This is particularly true in the context of web services, such as Tango.

Traditionally, this has been done by attaching a separate processor that receives the output from JAXB, then do c14n, then write it out to byte stream (and for a reason beyond me they chose to do this by building a DOM, instead of doing it in a streaming fashion, but that's a different story.)

Anyway, even if you do c14n in a streaming filter, it still has to pass through an infoset API, such as StAX or SAX. But since JAXB can write XML directly to UTF-8 byte stream very fast, we wanted to take advantage of this even when we are doing c14n. Besides, JAXB knows a lot about its output --- for example, it knows how it produces namespace declarations, it knows that it doesn't produce CDATA, etc. All these knowledge allows JAXB to do c14n faster than doing so as a general-purpose filter.

So, here's how to do it. Just insert the following code before you do the marshalling:

marshaller.setProperty("com.sun.xml.bind.c14n",true);

Note that this is still an experimental feature. There are different modes of c14n, such as inclusive vs exclusive, and I don't claim I understand all the details, so there could be some bugs, too. I appreciate any feedback.

Related Topics >>