Thom Nichols

Thom

Technology is evolution outside the gene pool

Using Smack XMPP with JAXB

So it I've looked a few times in the past, trying to find a decent way to use JAXB with the Smack XMPP library for Java.  More to the point, I wanted to write a Smack provider to parse a custom IQ message, but use JAXB to unmarshal the IQ body.  Should be doable. 

Nobody on the Smack forums had seemed to figure out a way to do it.  The reason it's so difficult is, Smack uses the XmlPull v1 API, which is the predecessor to StAX.  (StAX is what JAXB generally uses, but the APIs are not compatible.)

Well, after a bunch more searching, I finally stumbled into Sun's JAX-WS example code.  Hidden in there is this gem: PullUnmarshaller.java.  It adapts an XmlPull v1 parser to JAXB!  Perfect, my work is done!  Well, not quite.  Initial behavior indicated that I was getting a single message into my plugin, and then nothing after that.  What gives?  After some debugging, it turns out that PullUnmarshaller is consuming the closing tag event, which Smack doesn't like.

So, you have to modify PullUnmarshaller and comment out this one line:

250.
// skips the last end element.
251.pp.next();

Do that, and PullUnmarshaller works wonderfully!  I hope to post a fully working example soon, I'll post a link here when I do.

Category: Java XMPP