xml-parsingjaxbxmladapter

JAXB - adapter for a library object


I want to read an integer (inside XML element) to a Semaphore instead of int, effectively calling Semaphore(int theInteger) . Problem is - Semaphore doesn't have a default constructor. If it was a class I wrote I could either make a no-arg private constructor or write an adapter, but since I have no access to Semaphore - what can I do?


Solution

  • An object that you are adapting with an XmlAdapter doesn't need to a no-art constructor. You could set it up as follows:

    SemaphoreAdapter extends XmlAdapter<Integer, Semaphore>
    

    Then you would use the @XmlJavaTypeAdapter annotation on the property of type Semaphore to reference the XmlAdapter.