users@jaxb.java.net

How to marshal hexBinary correctly?

From: Liu, Jervis <jliu_at_iona.com>
Date: Tue, 31 Jul 2007 10:53:31 -0400

Hi,

I have a hexBinary type in my schema and I use code below to do the marshaling. In this test case the byte array comprises of {-128, 0, 127) which should be encoded as 80007F, but the result is gAB. I think I probably should use HexBinaryAdapter, but calling jm.setAdapter(new HexBinaryAdapter()) doesn't¡¯t help. The java doc says setAdapter only registers a HexBinaryAdapter on a hash map with the key of HexBinaryAdapter.class, you need an annotation of @XmlJavaTypeAdapter(HexBinaryAdapter.class) to have this adaptor triggered. However in my case, I do not have any Java type classes, thus there is no way to have an annotation. Any suggestions or comments will be highly appreciated.

            byte[] arg = {-128, 0, 127};
            Marshaller jm = JAXBContext.newInstance(arg.getClass()).createMarshaller();
            jm.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            jm.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            jm.marshal(new JAXBElement(new QName("myns", "myHexBinary"), arg.getClass(), arg), System.out);

Thanks,
Jervis

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland